(Dice Rolling with vector) Modify the dice-rolling program you created in Exercise 6.17 to use a vector to store the numbers of times each possible sum of the two dice appears.
What will be an ideal response?
```
#include
#include
#include
#include
#include
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 };
int x; // first die
int y; // second die
vector< int > sum( SIZE );
srand( time( 0 ) );
// roll dice 36,000 times
for ( long 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\n" << 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 << "%\n";
} // end main
```
Sum Total Expected Actual
2 997 2.778% 2.769%
3 1982 5.556% 5.506%
4 3032 8.333% 8.422%
5 4064 11.111% 11.289%
6 4968 13.889% 13.800%
7 5927 16.667% 16.464%
8 5037 13.889% 13.992%
9 4014 11.111% 11.150%
10 2994 8.333% 8.317%
11 1990 5.556% 5.528%
12 995 2.778% 2.764%
You might also like to view...
What is the file extension for an Access 2016 database file?
A) .accdb B) .aacdb C) .mdb D) .dbf
Which of the following is not true of static local variables?
a. They are accessible outside of the function in which they are defined. b. They retain their values when the function in which they are defined terminates. c. They are initialized to zero if not explicitly initialized by the programmer. d. They can be of type int.