Write a program that does the same thing with adding up 2 6-sided
Fig 7.10 page 278 shows a program simulating rolling 6,000,000 times a 6-sided die.
Write a program that does the same thing with adding up 2 6-sided dice first (totals are between 2 and 12), and then adding up 3 4-sided dice (totals are between 3 and 12).
1 // Roll a six-sided die 6,000,000 times.
2 #include <iostream>
3 #include <iomanip>
4 #include <cstdlib>
5 #include <ctime>
6 using namespace std;
7
8 int main()
9 {
10 const int arraySize = 7; // ignore element zero
11 int frequency[ arraySize ] = {}; // initialize elements to 0
12
13 srand( time( 0 ) ); // seed random number generator
14
15 // roll die 6,000,000 times; use die value as frequency index
16 for ( int roll = 1; roll <= 6000000; ++roll )
17 ++frequency[ 1 + rand() % 6 ];
18
19 cout << "Face" << setw( 13 ) << "Frequency" << endl;
20
21 // output each array element's value
22 for ( int face = 1; face < arraySize; ++face )
23 cout << setw( 4 ) << face << setw( 13 ) << frequency[ face ]
24 << endl;
25 } // end main
12 years ago
Purchase the answer to view it

- dieroll.cpp