rand() to roll the first die and should use rand() again to roll the second die.
The sum of the two values should then be calculated. Your program should
roll the dice 120,000 times. Tally the numbers of
times each possible sum appears. Print the results in 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.
the output must come out like this:
Sum Total Expected Actual
2 992 2.778% 2.756%
3 1936 5.556% 5.378%
4 3000 8.333% 8.333%
5 3981 11.111% 11.058%
6 4962 13.889% 13.783%
7 6036 16.667% 16.767%
8 5126 13.889% 14.239%
9 4076 11.111% 11.322%
10 2933 8.333% 8.147%
11 1953 5.556% 5.425%
12 1005 2.778% 2.792%
and my program output come out like this:
Sum Total Expected Actual
2 5 0% 0%
3 5 0% 0%
4 5 0% 0%
5 5 0% 0%
6 5 0% 0%
7 5 0% 0%
8 5 0% 0%
9 5 0% 0%
10 5 0% 0%
11 5 0% 0%
12 5 0% 0%
here's my code:
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <iomanip>
int Sum, Expected, Actual;
using namespace std;
int main() {
int Dice1;
int Dice2;
int Total = 0;
srand(time(0));
cout << "Sum" << setw(10) << "Total" << setw(13) << "Expected" << setw(12) << "Actual";
cout << endl;
for (int i = 0; i <= 120000; i++) {
Dice2= 1 + rand() % 9;
Dice1= 1 + rand() % 9;
Total = Dice1 + Dice2;
Total++;
}
for (int r = 2; r <= 12; r++) {
cout << r << setw(10) << Total << setw(13) << setprecision(3) << r / 36 << " %" << setw(12);
cout << setprecision(3) << r /120000 << " %";
cout << endl;
}
system("pause");
return 0;
}

New Topic/Question
Reply



MultiQuote






|