c++ 打印方块图直方图

xlpyo6sf  于 2023-01-28  发布在  其他
关注(0)|答案(1)|浏览(152)

当我编译并运行我的代码时,尽管有一个随机函数,但每次都得到相同的输出。它默认为任务指南上显示的试用输出。我试图改变print_histogram中的内容,但每次我在终端中得到消息说我没有声明“blank”。

// ============================================================================
// diceplot.cpp
//
//
// ============================================================================

#include <iostream>
#include <cstdlib>
using namespace std;

// Prototype/define functions anywhere below
//loop that rolls dice and returns the dice number
int roll() {    
    int dice = rand() % 6 + 1;
    return dice;    
}   
//loop that arranges the numbers and prints it out starting from index
void print_histogram(int list[], const int number_rolls){
    for (int count = 0; count < number_rolls; count++){
        cout << "\n" << count + 4 << ": ";
        for (int index = 0; index < list[count]; index++){
                cout << "X";
            }
    }
}
//given code
int main() {
  int seed, n;
    cout << "Enter a positive interger: " << endl;
  cin >> seed >> n;
  // Seed the pseudo-random number generator for repeatable results
  srand(seed);
  // Your code here
    // keeps track of the outputs from the rolls
    int list[21];
        for (int count = 0; count < 21; count ++){
        list[count] = count / 2;
    }
    //adds the roll of each experiment 
    int sum = 0; 
    for(int count = 0; count < n; count++) {        
        int dice_1 = roll();
        int dice_2 = roll();
        int dice_3 = roll();
        int dice_4 = roll();
        sum = dice_1 + dice_2 + dice_3 + dice_4;
        if (sum == 21){
            list[25 - 4]++;
            break;
        }
    }
// prints out the histogram
    print_histogram(list, 21);
  return 0;
}
hmae6n7t

hmae6n7t1#

我复制了您发布的代码,对其进行了格式化,删除了所有注解,然后添加了我自己的注解:

#include <iostream>
#include <cstdlib>
using namespace std;

int roll() {
    int dice = rand() % 6 + 1;
    return dice;
}

void print_histogram(int list[], const int number_rolls) {
    for (int count = 0; count < number_rolls; count++) {
        cout << "\n" << count + 4 << ": ";
        for (int index = 0; index < list[count]; index++) {
            cout << "X";
        }
    }
}

int main() {
    int seed, n;
    cout << "Enter a positive interger: " << endl;
    cin >> seed >> n;

    // Here srand is called which does indeed seed the calls to rand. However,
    // that doesn't change your output because your output is not showing
    // anything that has to do with rand being called
    srand(seed);

    // This has exactly 21 elements, not n elements. Not sure if that was
    // something you wrote or a default from your assignment
    int list[21];

    for (int count = 0; count < 21; count++) {
        // This right here is what your histogram is currently showing. There
        // is nothing random here, the value is the same every time
        list[count] = count / 2;
    }
    int sum = 0;
    for (int count = 0; count < n; count++) {
        int dice_1 = roll();
        int dice_2 = roll();
        int dice_3 = roll();
        int dice_4 = roll();
        sum = dice_1 + dice_2 + dice_3 + dice_4;

        // I don't know why the condition exists. As the code is currently
        // written the dice rolls essentially have a chance to add up to 21 and
        // cause the code inside the if statement to be executed
        if (sum == 21) {
            // 25 minus 4 is 21. Writting to index 21 is out of bounds because
            // list only has 21 elements. This is undefined behavior
            list[25 - 4]++;
            break;
        }
    }
    print_histogram(list, 21);
    return 0;
}

我建议删除此部分:

for (int count = 0; count < 21; count++) {
    // This right here is what your histogram is currently showing. There
    // is nothing random here, the value is the same every time
    list[count] = count / 2;
}

...并将其替换为将list的所有元素设置为0的代码。稍后,在负责掷四个骰子的for循环中,您可以根据掷出的骰子数之和递增list的元素
去掉这个:

if (sum == 21) {
    // 25 minus 4 is 21. Writting to index 21 is out of bounds because
    // list only has 21 elements. This is undefined behavior
    list[25 - 4]++;
    break;
}

...并将其替换为递增list的相应元素的代码,而不管sum的值是什么

相关问题