這個程序的目的是爲了讓用戶聲明他們的數組的大小,然後使用它的各種函數。我的問題是我發現在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()'的有效變量。 –