C++ 将每个值传递到数组中
我目前一直坚持将值存储到数组中 下面是我的进度。我想将每个随机生成的数字存储到一个数组中 我刚刚创建了一个新的函数原型,据推测,它将读取生成的每个数字并存储在其数组中C++ 将每个值传递到数组中,c++,arrays,C++,Arrays,我目前一直坚持将值存储到数组中 下面是我的进度。我想将每个随机生成的数字存储到一个数组中 我刚刚创建了一个新的函数原型,据推测,它将读取生成的每个数字并存储在其数组中 #include<iostream> using namespace std; //Function prototype void random(int val); void store(int val1); int main() { int nvalue; cout << "How
#include<iostream>
using namespace std;
//Function prototype
void random(int val);
void store(int val1);
int main()
{
int nvalue;
cout << "How many numbers would you like to generate?\n";
cin >> nvalue;//get input from user
random(nvalue);//pass user input into random() function
system("pause");
return 0;
}
void random(int val)
{
int num;//represent random integer output
for (int i = 1; i < val; i++)//loop that will continue to generate integers based on n value from user
{
num = rand() % val + 1;//randomly generate number
cout << "Num [" << i << "]" << "is " << num<<endl;
}
}
#包括
使用名称空间std;
//功能原型
无效随机(int-val);
无效存储(int val1);
int main()
{
国际价值;
cout>nvalue;//从用户获取输入
random(nvalue);//将用户输入传递给random()函数
系统(“暂停”);
返回0;
}
无效随机(int-val)
{
int num;//表示随机整数输出
for(int i=1;i cout以下是您需要的实现示例:
void random_store(int val, vector<int> &aVec);
int main()
{
int nvalue;
cout << "How many numbers would you like to generate?\n";
cin >> nvalue;//get input from user
vector<int> int_vector;
random_store(nvalue, int_vector);//pass user input into random() function
system("pause");
return 0;
}
void random_store(int val, vector<int> &aVec)
{
int num;//represent random integer output
for (int i = 0; i < val; i++)
{
aVec.push_back(rand() % val + 1);
cout << "Num [" << i << "]" << "is " << aVec[i] <<endl;
}
}
void随机存储(int-val、vector和aVec);
int main()
{
国际价值;
cout>nvalue;//从用户获取输入
向量int_向量;
random_store(nvalue,int_vector);//将用户输入传递到random()函数中
系统(“暂停”);
返回0;
}
无效随机存储(int-val、vector和aVec)
{
int num;//表示随机整数输出
对于(int i=0;i cout以下是您需要的实现示例:
void random_store(int val, vector<int> &aVec);
int main()
{
int nvalue;
cout << "How many numbers would you like to generate?\n";
cin >> nvalue;//get input from user
vector<int> int_vector;
random_store(nvalue, int_vector);//pass user input into random() function
system("pause");
return 0;
}
void random_store(int val, vector<int> &aVec)
{
int num;//represent random integer output
for (int i = 0; i < val; i++)
{
aVec.push_back(rand() % val + 1);
cout << "Num [" << i << "]" << "is " << aVec[i] <<endl;
}
}
void随机存储(int-val、vector和aVec);
int main()
{
国际价值;
cout>nvalue;//从用户获取输入
向量int_向量;
random_store(nvalue,int_vector);//将用户输入传递到random()函数中
系统(“暂停”);
返回0;
}
无效随机存储(int-val、vector和aVec)
{
int num;//表示随机整数输出
对于(int i=0;i cout以下是您需要的实现示例:
void random_store(int val, vector<int> &aVec);
int main()
{
int nvalue;
cout << "How many numbers would you like to generate?\n";
cin >> nvalue;//get input from user
vector<int> int_vector;
random_store(nvalue, int_vector);//pass user input into random() function
system("pause");
return 0;
}
void random_store(int val, vector<int> &aVec)
{
int num;//represent random integer output
for (int i = 0; i < val; i++)
{
aVec.push_back(rand() % val + 1);
cout << "Num [" << i << "]" << "is " << aVec[i] <<endl;
}
}
void随机存储(int-val、vector和aVec);
int main()
{
国际价值;
cout>nvalue;//从用户获取输入
向量int_向量;
random_store(nvalue,int_vector);//将用户输入传递到random()函数中
系统(“暂停”);
返回0;
}
无效随机存储(int-val、vector和aVec)
{
int num;//表示随机整数输出
对于(int i=0;i cout以下是您需要的实现示例:
void random_store(int val, vector<int> &aVec);
int main()
{
int nvalue;
cout << "How many numbers would you like to generate?\n";
cin >> nvalue;//get input from user
vector<int> int_vector;
random_store(nvalue, int_vector);//pass user input into random() function
system("pause");
return 0;
}
void random_store(int val, vector<int> &aVec)
{
int num;//represent random integer output
for (int i = 0; i < val; i++)
{
aVec.push_back(rand() % val + 1);
cout << "Num [" << i << "]" << "is " << aVec[i] <<endl;
}
}
void随机存储(int-val、vector和aVec);
int main()
{
国际价值;
cout>nvalue;//从用户获取输入
向量int_向量;
random_store(nvalue,int_vector);//将用户输入传递到random()函数中
系统(“暂停”);
返回0;
}
无效随机存储(int-val、vector和aVec)
{
int num;//表示随机整数输出
对于(int i=0;i cout数组在程序中的什么位置?然后创建一个数组并将它(通过引用)传递给函数,然后将数字添加到向量中。还要注意的是,您不是在生成nvalue
随机数,而是nvalue-1
。正如Joachim所说:void random(int-val,std::vector&array);
除了将数组作为参数传递给函数外,还可以返回向量。数组在程序中的位置?然后创建一个数组并传递它(通过引用),并将数字添加到向量中。还要注意,您并不是生成nvalue
随机数,而是生成nvalue-1
。正如Joachim所说:void random(int-val,std::vector&array);
除了将数组作为参数传递给函数外,还可以返回向量。数组在程序中的位置?然后创建一个数组并传递它(通过引用),并将数字添加到向量中。还要注意,您并不是生成nvalue
随机数,而是生成nvalue-1
。正如Joachim所说:void random(int-val,std::vector&array);
除了将数组作为参数传递给函数外,还可以返回向量。数组在程序中的位置?然后创建一个数组并传递它(通过引用)要注意的是,您不是在生成nvalue
随机数,而是在生成nvalue-1
。正如Joachim所说:void random(int val,std::vector&array);
您也可以返回一个向量,而不是将数组作为参数传递给函数。