Question: Need help with this c++ program template. If you could also add comments to help me understand and study it, I would be very grateful.
Need help with this c++ program template. If you could also add comments to help me understand and study it, I would be very grateful. Thank you.
#includeusing std::cout; using std::ios; #include using std::setw; using std::setprecision; using std::fixed; using std::showpoint; #include using std::rand; using std::srand; #include using std::time; 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 /* Write the declaration of array expected here. Assign an initializer list containing the expected values here. Use SIZE for the number of elements */ // expected[0] and expected[1] will have the value of 0 // since there are zero ways for two dice to add up to 0 or 1 // expected[2] will hold 1, 1 + 1 gives 2 // expected[3] will hold 2, 1 + 2 and 2 + 1 both gives 3 // expected[4] will hold 3, 1 + 3 and 3 + 1 and 2 + 2 all gives 4 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 */ // see page 383 of the text for initializing arrays at compile time 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 << "% "; return 0; // indicates successful completion } // end main
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
