2014-03-07 45 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我显示的代码也没有算法。但是关于算法的信息也是有用的。 –