//dice.cpp #include #include #include #include using namespace std; int main() {
ID: 3627080 • Letter: #
Question
//dice.cpp
#include
#include
#include
#include
using namespace std;
int main()
{
const long ROLLS = 36000;
const int SIZE = 13;
// array exepected contains counts for the expected
// number of times each sum occurs in 36 rolls of the dice
/* Write the declaration of array exprected here. Assign an
initializer list containing the expected values here. Use
SIZE for the number of elements */
int x; // first die
int y; // second die
/* Write declaration for array sum here. Initialize all
elements to zero. Use SIZE for the number of elements */
srand( time( 0 ) );
// roll dice 36,000 times
/* Write a for loop that iterates ROLLS times. Randomly
generate values for x (i.e., die1) and y (i,e, die2)
and increment the appropriate counter in array sum that
corresponds to the sum of x and y */
cout << setw( 10 ) << "Sum" << setw( 10 ) << "Total" << setw( 10 )
<< "Expected" << setw( 10 ) << "Actual " << fixed << showpoint;
// display results of rolling dice
for ( int j = 2; j < SIZE; j++ )
cout << setw( 10 ) << j << setw( 10 ) << sum[ j ]
<< setprecision( 3 ) << setw( 9 )
<< 100.0 * expected[ j ] / 36 << "%" << setprecision( 3 )
<< setw( 9 ) << 100.0 * sum[ j ] / 36000 << "% ";
System ("pause") ;
return 0;
} // end main
Explanation / Answer
//dice.cpp
#include<iostream>
#include <stdlib.h>
#include <time.h>
#include<iomanip>
using namespace std;
int main()
{
const long ROLLS = 36000;
const int SIZE = 13;
/* array expected contains counts for the expected
number of times each sum occurs in 36 rolls of the dice */
int expected[ SIZE ] = { 0, 0, 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1};
// array exepected contains counts for the expected
// number of times each sum occurs in 36 rolls of the dice
/* Write the declaration of array exprected here. Assign an
initializer list containing the expected values here. Use
SIZE for the number of elements */
int x; // first die
int y; // second die
/* Write declaration for array sum here. Initialize all
elements to zero. Use SIZE for the number of elements */
int sum[SIZE] = { 0 };
srand( time( 0 ) );
// roll dice 36,000 times
/* Write a for loop that iterates ROLLS times. Randomly
generate values for x (i.e., die1) and y (i,e, die2)
and increment the appropriate counter in array sum that
corresponds to the sum of x and y */
for (int i = 1; i <= ROLLS; i++ ) {
x = 1 + rand() % 6;
y = 1 + rand() % 6;
++sum[ x + y ];
} /* end for */
cout << setw( 10 ) << "Sum" << setw( 10 ) << "Total" << setw( 10 )
<< "Expected" << setw( 10 ) << "Actual " << fixed << showpoint;
// display results of rolling dice
for ( int j = 2; j < SIZE; j++ )
cout << setw( 10 ) << j << setw( 10 ) << sum[ j ]
<< setprecision( 3 ) << setw( 9 )
<< 100.0 * expected[ j ] / 36 << "%" << setprecision( 3 )
<< setw( 9 ) << 100.0 * sum[ j ] / 36000 << "% ";
return 0;
} // end main
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.