【发布时间】:2013-11-28 17:57:20
【问题描述】:
以下是整个问题。
编写一个模拟掷两个骰子的程序。该程序 应该使用 rand 来滚动第一个骰子,并且应该再次使用 rand 掷第二个骰子。然后应计算两个值的总和。 [注:每个骰子可以显示一个从 1 到 6 的整数值,所以总和 这两个值将在 2 到 12 之间变化,其中 7 是最常见的 sum 和 2 和 12 是最不频繁的和。] 请注意,有 两个骰子的 36 种可能组合。你的程序应该滚动 两个骰子 3,600 次。使用一维数组来计算数字 每个可能的总和出现的次数。以表格形式打印结果 格式。此外,确定总数是否合理(即,有 掷 7 的六种方法,因此大约是所有掷骰的六分之一 应该是 7)。
结果应该如下:
Question 2
Please enter the seed : 2
我不知道如何生成“预期”列。
这是我的程序:(主要是Q2_main())
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
double total_Array[11];
double expected_Array[11];
double actual_Array[11];
int seed;
void initialization_of_Array()
{
for (int counter=0; counter < 12; counter++)
{
total_Array[counter] = 0;
expected_Array[counter] = 0;
actual_Array[counter] = 0;
}
}
void show_heading_line()
{
cout << setw(5) << "Sum"
<< setw(10) << "Total"
<< setw(17) << "Expected"
<< setw(16) << "Actual"
<< endl;
}
void show_Data_Results_line(int sum, int total, double expected, double actual)
{
cout << setw(5) << sum
<< setw(10) << total
<< setw(16) << expected << "%"
<< setw(15) << actual << "%"
<< endl;
}
void calculation_of_total()
{
int die_1, die_2;
for (int counter = 1; counter <= 3600; counter++)
{
die_1 = 1 + rand() % 6;
die_2 = 1 + rand() % 6;
total_Array[((die_1 + die_2)-2)]++;
}
}
void calculation_of_expect()
{
}
void calculation_of_actual()
{
for (int counter = 0; counter < 11; counter++)
{
actual_Array[counter] = (total_Array[counter] / 3600.0) * 100.0;
}
}
void rollDice_Operation()
{
calculation_of_total();
calculation_of_expect();
calculation_of_actual();
}
void print_Result()
{
show_heading_line();
for (int counter = 0; counter <= 10; counter++)
{
show_Data_Results_line((counter+2), total_Array[counter], 1, actual_Array[counter]);
}
}
void Q2_main()
{
cout << setprecision(3) << fixed;
initialization_of_Array();
cout << "Please enter the seed : ";
cin >> seed;
srand(seed);
rollDice_Operation();
print_Result();
}
谁能给我一些处理“预期”列的提示?
感谢您的关注
【问题讨论】:
-
这是一个编程问题还是您无法理解这个问题?
-
expected result不是计算结果,它是特定于问题的数学结果(两个骰子的总和)。您不必为该列计算任何内容。 -
我想我无法理解这个问题......
-
当有人在 c++ 中使用
rand时一如既往:randis considered harmful
标签: c++ random probability simulate dice