i need help i want to make this code shown in Fig 7.10 page 293 shows a program
ID: 3629909 • Letter: I
Question
i need help i want to make this code shown in Fig 7.10 page 293 shows a program simulating rolling 6,000,000 times a 6-sided die. i want to make it do the same thing with 2 6-sided dice first, and 3 4-sided dice. here is the code below.#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
const int arraySize = 7; // ignore element zero
int frequency[ arraySize ] = {}; // initialize elements to 0
srand( time( 0 ) ); // seed random number generator
// roll die 6,000,000 times; use die value as frequency index
for ( int roll = 1; roll <= 6000000; roll++ )
frequency[ 1 + rand() % 6 ]++;
cout << "Face" << setw( 13 ) << "Frequency" << endl;
// output each array element's value
for ( int face = 1; face < arraySize; face++ )
cout << setw( 4 ) << face << setw( 13 ) << frequency[ face ]
<< endl;
cout << " " <<endl;
// roll die 6,000,000 times; use die value as frequency index
for ( int roll = 2; roll <= 6000000; roll++ )
frequency[ 1 + rand() % 6 ]++;
for ( int face =1; face < arraySize; face++ )
cout << setw( 4 ) << face << setw( 13 ) << frequency[ face ]
<< endl;
system("PAUSE");
} // end main
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
const int arraySize = 7; // ignore element zero
// 7 for 1 die
// 13 for 2 die
// 19 for 3 die
int frequency[ arraySize ] = {}; // initialize elements to 0
srand( time( 0 ) ); // seed random number generator
// roll die 6,000,000 times; use die value as frequency index
for ( int roll = 1; roll <= 6000000; roll++ )
frequency[ 1 + rand() % 6 ]++;
// for 2 dice
//frequency[1 + rand() % 6 + 1 + rand() % 6 ]++;
//for 3 dice
//frequency[1 + rand() % 6 +1 + rand() % 6 + 1 + rand() % 6 ]++;
cout << "Face" << setw( 13 ) << "Frequency" << endl;
// output each array element's value
for ( int face = 1; face < arraySize; face++ )
cout << setw( 4 ) << face << setw( 13 ) << frequency[ face ]
<< endl;
cout << " " <
// roll die 6,000,000 times; use die value as frequency index
for ( int roll = 2; roll <= 6000000; roll++ )
frequency[ 1 + rand() % 6 ]++;
// for 2 dice
//frequency[1 + rand() % 6 + 1 + rand() % 6 ]++;
//for 3 dice
//frequency[1 + rand() % 6 +1 + rand() % 6 + 1 + rand() % 6 ]++;
for ( int face =1; face < arraySize; face++ )
cout << setw( 4 ) << face << setw( 13 ) << frequency[ face ]
<< endl;
system("PAUSE");
} // end main
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.