当我编译并运行我的代码时,尽管有一个随机函数,但每次都得到相同的输出。它默认为任务指南上显示的试用输出。我试图改变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;
}
1条答案
按热度按时间hmae6n7t1#
我复制了您发布的代码,对其进行了格式化,删除了所有注解,然后添加了我自己的注解:
我建议删除此部分:
...并将其替换为将
list
的所有元素设置为0
的代码。稍后,在负责掷四个骰子的for循环中,您可以根据掷出的骰子数之和递增list
的元素去掉这个:
...并将其替换为递增
list
的相应元素的代码,而不管sum
的值是什么