C++:如何将数据从迭代循环输出到列中

C++: How to output data into columns from an iteration loop

本文关键字:输出 循环 迭代 数据 C++      更新时间:2023-10-16

我无法弄清楚如何将运行循环两次生成的数据输出到两列中。

我生成 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