在查找数组中最大的数字时,数组的正确声明应该是什么



C++这是我在C++中查找数组中最大数的代码。当我在IDE中运行时,没有编译错误,但它没有给我输出。我认为问题出在第8行的数组声明中。我将数组声明从第8行替换为第11行,然后它在我的IDE中运行良好。所以我不明白为什么数组的声明在第8行不起作用?

#include <bits/stdc++.h>
using namespace std;
int largest_in_array(int a[], int n);
int main() // main function
{
int n; // User will enter the size of array 
int arr[n]; // Line 8
cout << "Enter the size of array: " << endl;
cin >> n;
// Line 11
cout << "nEnter the elements of array: " << endl;
for (int i = 0; i < n; i++) // This loop will run for each element of array that user wants to enter
{
cout << "Enter the " << (i + 1) << " element:";
cin >> arr[i];
cout << endl;
}
cout << "Elements are: [";
for (int i = 0; i < n; i++) // Prints the elements of array
{
// cout << "Enter the " << (i + 1) << " element:";
cout << arr[i] << " ";
// cout << endl;
}
cout << "]";
int res = largest_in_array(arr, n); //Function call
cout << "nLargest element in array is: " << arr[res] << endl;
return 0;
}
int largest_in_array(int a[], int n) // function that will return the index of largest element in array
{
int max = 0;
for (int i = 1; i < n; i++)
{
if (a[max] < a[i])
{
max = i;
}
}
return max;
} 
  • 在用户向n中输入值之前声明intarr[n];。当您读取并创建arr时,n的值不确定
  • 您不会检查用户是否在n中输入了正值。零和负大小的数组无效

其他要点:

  • bits/stdc++.h不是一个标准标头,这会使您的程序不可移植。使用适当的头文件,如iostream
  • arr[n]是一个可变长度阵列(VLA),它不是标准C++的一部分。改为std::vector<int> arr(n);
  • CCD_ 10的使用是不必要的。这里不需要刷新输出流。请改用n

示例:

#include <iostream>
#include <limits>
#include <vector>
int largest_in_array(const std::vector<int>& a) {
int max = 0;
for(int i = 1; i < a.size(); i++) {
if(a[max] < a[i]) {
max = i;
}
}
return max;
}
int main() // main function
{
int n; // User will enter the size of array
std::cout << "Enter the size of array:n";
// check that input succeeds and that the value is valid
if(!(std::cin >> n) || n < 1) return 1;
std::vector<int> arr(n);
std::cout << "nEnter the elements of array:n";
for(int i = 0; i < n; i++)
{
std::cout << "Enter the " << (i + 1) << " element:";
if(!(std::cin >> arr[i])) {
std::cout << "invalid input, bye byen";
return 1;
}
}
std::cout << "Elements are: [";
for(int i = 0; i < n; i++)
{
std::cout << arr[i] << " ";
}
std::cout << "]";
int res = largest_in_array(arr); // Function call
std::cout << "nLargest element in array is: " << arr[res] << 'n';
}

也就是说,您可以使用标准算法std::max_element,而不是编写自己的算法。它将迭代器返回到最大元素。当您不需要知道数组中的索引时,也可以使用基于范围的for循环,就像在第二个循环中一样。

示例:

#include <algorithm>
#include <cstddef>
#include <iostream>
#include <iterator>
#include <limits>
#include <vector>
int main() {
int n; // User will enter the size of array
std::cout << "Enter the size of array:n";
if(!(std::cin >> n) || n < 1) return 1;
std::vector<int> arr(n);
std::cout << "nEnter the elements of array:n";
for(int i = 0; i < n; i++) // This loop will run for each element of
// array that user wants to enter
{
std::cout << "Enter the " << (i + 1) << " element:";
if(!(std::cin >> arr[i])) {
std::cout << "invalid input, bye byen";
return 1;
}
}
std::cout << "Elements are: [";
for(auto value : arr) {          // a range-based for loop
std::cout << value << ' ';
}
std::cout << "]n";
auto res = std::max_element(arr.begin(), arr.end());
std::cout << "Largest element in array is: " << *res << 'n';
std::size_t index = std::distance(arr.begin(), res);
std::cout << "which has index " << index << 'n';
}

当第8行有int n时,当您使用它来创建数组时,它会被初始化。当显式初始化n时,其值为未定义行为。您可能创建了一个比您在第10行输入的n的数组,导致该数组具有额外的随机垃圾,它可能更小,这意味着您的程序读取的内存实际上不应该读取,等等。

最新更新