gpt4 book ai didi

c++ - 模拟掷 2 个骰子的程序

转载 作者:太空宇宙 更新时间:2023-11-04 16:18:23 25 4
gpt4 key购买 nike

下面是全题。

Write a program that simulates the rolling of two dice. The program should use rand to roll the first die and should use rand again to roll the second die. The sum of two values should then be calculated. [Note : Each die can show an integer value from 1 to 6, so the sum of the two values will vary from 2 to 12, with 7 being the most frequent sum and 2 and 12 being the least frequent sums.] Note that there are 36 possible combinations of the two dice. Your program should roll the two dice 3,600 times. Use a one_dimensional array to tally the numbers of times each possible sum appears. Print the results in a tabular format. Also, determine if the totals are reasonable (i.e., there are six ways to roll a 7, so approximately one-sixth of all the rolls should be 7).

结果应该是这样的:

Question 2
Please enter the seed : 2

enter image description here

我不知道如何生成“预期”列。 enter image description here

这是我的程序:(主要是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();
}

谁能给我一些提示来处理“预期”列?


感谢您的关注

最佳答案

预期列只是结果的数学概率:

+-------+-------------------------+--------------------+-------------+
| Value | Possibilities | # of possibilities | Probability |
+-------+-------------------------+--------------------+-------------+
| 2 | 1+1 | 1 | 1/36=2.78% |
| 3 | 1+2,2+1 | 2 | 2/36=5.56% |
| 4 | 1+2,2+2,2+1 | 3 | 3/36=8.33% |
| 5 | 1+4,2+3,3+2,4+1 | 4 | 4/36=11.11% |
| 6 | 1+5,2+4,3+3,4+2,5+1 | 5 | 5/36=13.89% |
| 7 | 1+6,2+5,3+4,4+3,5+2,6+1 | 6 | 6/36=16.67% |
| 8 | 2+6,3+5,4+4,5+3,6+2 | 5 | 5/36=13.89% |
| 9 | 3+6,4+5,5+4,6+3 | 4 | 4/36=11.11% |
| 10 | 4+6,5+5,6+4 | 3 | 3/36=8.33% |
| 11 | 5+6,6+5 | 2 | 2/36=5.56% |
| 12 | 6+6 | 1 | 1/36=2.78% |
+-------+-------------------------+--------------------+-------------+

不用计算,打印出来与实际统计结果比较:

double expected_Array[11] = {1/.36, 2/.36, 3/.36, 4/.36, 5/.36, 6/.36, 5/.36, 4/.36, 3/.36, 2/.36, 1/.36};
...
show_Data_Results_line((counter+2), total_Array[counter], expected_Array[counter], actual_Array[counter]);

关于c++ - 模拟掷 2 个骰子的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19999418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com