这个程序的目的是为了让用户声明他们的数组的大小,然后使用它的各种函数。我的问题是我发现在FunctionTwo中没有任何有效的声明。我怎样才能从我的主要功能获取FunctionTwo等信息以实现其他功能?对函数和数组进行操作
int main()
{
srand(time(NULL));
int arraySize = 0;
cout << "How large would you like your array to be?" << endl;
cin >> arraySize;
int theArray [arraySize];
int selection = 0;
cout << "What would you like to do with your array? " << endl << endl;
cout << "1. Pass in an integer location and return the value. " << endl;
cout << "2. Initialize an array of all 0's. " << endl;
cout << "3. Initialize an array of random numbers between 1 and your specification. " << endl;
cout << "4. Populate an array one at a time. " << endl;
cout << "5. Select a position in the array and set that value to your specification. " << endl;
cout << "6. Print the entire array. " << endl;
cout << "7. Find the average of each value in the array. " << endl;
cout << "8. Find the largest element of the array. " << endl;
cout << "9. Find the smallest element of the array. " << endl;
cout << "12. Print all numbers in the array larger than your input. " << endl;
cout << "13. Tell if the array is empty. " << endl;
cout << "15. Return the difference between the largest and smallest value in the array. " << endl;
cin >> selection;
}
int FunctionTwo()
{
int theArray [arraySize] = {0};
return theArray;
}
C++中的基本数组索引大小必须在编译时声明。为了确定运行时的数组大小,你应该考虑使用'std :: vector'来代替。 –
你是什么意思“FunctionTwo中没有声明”? –
@DanielDaranas我认为他的意思是在范围内没有'main()'的有效变量。 –