2014-03-07 88 views
0

這個程序的目的是爲了讓用戶聲明他們的數組的大小,然後使用它的各種函數。我的問題是我發現在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; 
} 
+3

C++中的基本數組索引大小必須在編譯時聲明。爲了確定運行時的數組大小,你應該考慮使用'std :: vector '來代替。 –

+2

你是什麼意思「FunctionTwo中沒有聲明」? –

+1

@DanielDaranas我認爲他的意思是在範圍內沒有'main()'的有效變量。 –

回答

1

int theArray [arraySize];有效的C++如果arraySize是在運行時唯一已知的。

您的問題在C++中正確的解決辦法:

std::vector<int> theArray(arraySize); 
-1

我怎樣才能從我的主要功能信息FunctionTwo等了我的其他功能呢?

您可以提供函數參數。例如:

int FunctionTwo(std::vector<int>& the_vector) 

然後在main()

switch (selection) 
{ 
    case 2: FunctionTwo(the_vector); break; 
    ...other cases... 
} 
-1
int FunctionTwo(int[] theArray) {} 

這將允許您使用theArray函數內。希望這是你的意思。我有點不清楚你想做什麼。

現在,可以調用該功能的這樣:

FunctionTwo(anArray); 
0

在C++中的陣列的大小應爲在編譯時已知的常量表達式。因爲變量ARRAYSIZE不是一個常量表達式此代碼

int arraySize = 0; 
    cout << "How large would you like your array to be?" << endl; 
    cin >> arraySize; 
    int theArray [arraySize]; 

不是C++標準。

因此,無論您將設置自己的數組中的一個固定的大小,而不要求用戶指定大小,例如

const int arraySize = 20; 
    int theArray [arraySize]; 

,或者必須動態分配陣列,例如

int arraySize = 0; 
    cout << "How large would you like your array to be?" << endl; 
    cin >> arraySize; 
    int *theArray = new int[arraySize]; 

在這種情況下,不要忘記退出程序之前刪除數組:

delete []theArray; 

您可以使用標準容器std::vector<int>,但我相信您的任務需要使用數組。

至於FunctionTwo的函數調用,那麼函數必須有兩個參數:指向數組的第一個元素的指針和數組中的元素數。沒有意義的聲明其返回類型爲INT,這將是最好將其聲明爲void下面一個例子

void FunctionTwo(int theArray[], int arraySize) 
{ 
    for (int i = 0; i < arraySize; i++) theArray[i] = 0; 
} 

或者你可以使用標準算法std::fill。例如

#include <algorithm> 

//... 

void FunctionTwo(int theArray[], int arraySize) 
{ 
    std::fill(theArray, theArray + arraySize, 0); 
} 

這裏是具有與隨機數的數組初始化的範圍內的所謂FunctionThree的示例[1,n]的

void FunctionThree(int theArray[], int arraySize, int n) 
{ 
    for (int i = 0; i < arraySize; i++) theArray[i] = std::rand() % n + 1; 
} 

或者相同,但使用標準算法std:::generate

#include <algorithm> 

//... 

void FunctionThree(int theArray[], int arraySize, int n) 
{ 
    std::generate(theArray, theArray + arraySize, [&] { return (std::rand() % n + 1); }); 
} 

考慮到函數應該在它們的使用之前被聲明。

至於它應該被包含在一個循環中的菜單。例如

do 
{ 
    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; 
    cout << "\n0. Exit from the program" << endl; 

    cin >> selection; 

    //...some other code 
} while (selection != 0); 
+0

如果賦值確實需要數組,那麼我相信標準算法也是不允許的。 –

+0

@Christian Hackl我顯示的代碼也沒有算法。但是關於算法的信息也是有用的。 –