【发布时间】:2022-11-13 21:44:00
【问题描述】:
我想创建一个名为 fillArray 的函数来为数组设置随机值。 然后我想让函数输出所有值,但我一直得到高于 99 的值。
这是我到目前为止所写的:
#include <ctime>
#include <iostream>
using namespace std;
void fillArray(){
srand(time(NULL));
const unsigned int sizeOfArray = 10; // this sets the array to the constant size of 10
int numberArray[sizeOfArray]; //this sets the array to the size of 'sizeOfArray' which is 10
for(int i = 0; i < sizeOfArray; i++)
{
numberArray[i] = rand() % 100;
cout << numberArray[i] << endl;
}
}
int main (){
void fillArray(int a[10], int randNum);
cout << numberArray[0] << " , " << numberArray[1] << " , " << numberArray[2] << " , " << numberArray[3] << " , " << numberArray[4] << " , " << numberArray[5] << " , " << numberArray[6] << " , " << numberArray[7] << " , " << numberArray[8] << " , " << numberArray[9] << '\n';
}
我知道名称 numberArray 尚未在主函数中声明,但只是想知道该怎么做。
【问题讨论】:
-
使用
void fillArray(int a[10], int randNum);你实际上并没有称呼功能。你宣布功能。它与您之前声明的void fillArray()函数不同。或许您需要刷新教科书中有关函数以及如何调用它们的章节? -
而不是
cout << numberArray[0] << " , " << numberArray[1] ...,为什么不使用循环呢?