C++:如何将数据从迭代循环输出到列中
C++: How to output data into columns from an iteration loop
我无法弄清楚如何将运行循环两次生成的数据输出到两列中。
我生成 100 个随机掷骰子,并输出每次掷骰子的次数。我这样做了两次。
我的目标是获取两组计数并输出它,使其看起来像:
"一:(第一盘数) (第二盘数)
二:(第一盘计数)(第二组计数)
三:(第一盘计数)(第二组计数)
等等...
这是我下面的代码:
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
int seed, random, count_one (0), count_two(0), count_three(0), count_four(0), count_five(0), count_six(0);
cout<<"Input the random seed: ";
cin>> seed;
srand(seed);
for(int i=0; i<2; i++) {
for (int i=0; i<100; i++) {random=(rand()%6)+1;
if (random==1)
count_one++;
if (random==2)
count_two++;
if (random==3)
count_three++;
if (random==4)
count_four++;
if (random==5)
count_five++;
if (random==6)
count_six++;
}
cout<<"First Set"<<"t"<<"Second Set "<<endl;
cout<<"one "<<count_one<<endl;
cout<<"two "<<count_two<<endl;
cout<<"three "<<count_three<<endl;
cout<<"four "<<count_four<<endl;
cout<<"five "<<count_five<<endl;
cout<<"six "<<count_six<<endl;
cout<<" Set 1 Avg."<<"t"<<"Set 2 Avg. "<<endl;
}
return 0;
}
任何帮助将不胜感激!!非常感谢:)
您需要为每个集合和数字提供一个计数变量。然后,您可以简单地将输出放在 for 循环之后,从而生成列的外观。
可能您想为此使用二维数组,因为这对您有两个直接的好处:
- 变量的定义更短
- 您不需要 if 说明。
这将类似于这样:
int seed;
int count[2][6] = {{0,0,0,0,0,0},{0,0,0,0,0,0}};
cout<<"Input the random seed: ";
cin>> seed;
srand(seed);
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 100; ++j) {
count[i][rand() % 6]++;
};
};
cout << "First Set" << "t" << "Second Set " << endl;
cout << "one " << count[0][0]<< "t" << count[1][0] << endl;
cout << "two " << count[0][1]<< "t" << count[1][1] << endl;
...
进一步的改进是将数字名称的字符串也放在一个数组中。因此,您可以放置大部分输出
由于您使用的是 c++,因此您也可以使用向量。我会将循环到一百的循环移动到一个函数中。该函数运行并使用计算的计数填充向量,然后仅调用该函数两次。之后,您可以运行打印例程:
#include <iostream>
#include <vector>
#include <stdlib.h>
using namespace std;
float getCounts (vector<int> &counts) {
float average = 0.0f;
// Initialize all counts to be output to zero
counts.clear();
for (int idxDieNumber=0; idxDieNumber<6; idxDieNumber++) {
counts.push_back(0);
}
// The run the loop you already had, but instead of printing here,
// just save the results in the vector passed
for (int i=0; i<100; i++) {
int random=(rand()%6)+1;
// instead of the original code: if (random==1) counts_one++; , etc
counts[random-1]++; // counts[0] corresponds to die face 'one', etc
average += random;
}
return average/100;
}
int main()
{
// we substitute count_one, count_two, etc in your code
// by two vectors, each stores the count in each loop
vector<int> countsPass1;
vector<int> countsPass2;
float averagePass1, averagePass2;
int seed;
cout<<"Input the random seed: ";
cin>> seed;
srand(seed);
// Instead of the two loops, call the function twice, passing the
// desired vector and the average for each pass
averagePass1 = getCounts (countsPass1);
averagePass2 = getCounts (countsPass2);
cout<<"First Set"<<"t"<<"Second Set "<<endl;
cout<<"one "<< countsPass1[0] << "t" <<countsPass2[0] << endl;
cout<<"two "<< countsPass1[1] << "t" <<countsPass2[1] << endl;
cout<<"three "<< countsPass1[2] << "t" <<countsPass2[2] << endl;
cout<<"four "<< countsPass1[3] << "t" <<countsPass2[3] << endl;
cout<<"five "<< countsPass1[4] << "t" <<countsPass2[4] << endl;
cout<<"six "<< countsPass1[5] << "t" <<countsPass2[5] << endl;
cout<<" Set 1 Avg. " << averagePass1 << "t"<<"Set 2 Avg. "<< averagePass2 <<endl;
return 0;
}
请注意,如果系统中的RAND_MAX
不是 6 的精确倍数(很可能不是),则平均值可能会偏离3
。
相关文章:
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- 为什么在C的循环中使用printf的Rust代码不显示输出,而在C++的循环中显示std::cout
- 如何读取单个字符并在输入两个字符序列时输出? 使用 while 循环和C++
- 遇到此问题时遇到困难:允许用户输入数组的值并使用 for,而循环也输出输入的最大数字
- 而循环:简单的除法程序输出零,不明白为什么
- 为什么即使使用 for 循环遍历我的向量,它也没有输出到控制台?(C++)
- 如何在 c++ 中使用 for 循环输出矢量中的所有对象
- 我将如何保存此 for 循环的输出
- 该程序没有产生输出,而是卡在一个循环中,几秒钟后消失并打印被杀死?
- 循环C++不同的输出
- 在控制台上输出我从文件中读取的整数,将程序插入无限循环
- 创建一个循环,该循环将输出大于零且小于 60(不包括 60)的所有 5 的倍数
- 为什么当 while 循环中的 if 条件变为 false 时,我的函数不输出最后一条语句?
- 当输出达到某个值时,如何在 c++ 中中断计数器循环
- 尝试使用嵌套的 for 循环进行某些输出
- 循环内控制台中的输入和输出
- while 循环中的流输出文件() 通常生成空文件
- C++,程序输出无效输入,但继续通过循环
- 当我在循环内打印变量时无限输出
- 如何水平排列程序的打印输出循环而不重复