Hey everybody, I seem to be stuck on this rolling dice problem I received on one of my lab exercises. Can somebody get me started on the right path. Here's a description of the problem along with the code given. The green indicates where I need to insert code.

DESCRIPTION: Write a program that simulates the rolling of two dice. The program should use rand to roll the first die and should use rand again to roll the second die. The sum of the two values should then be calculated. [ Note: Each die can show an integer value from 1 to 6, so the sum of the two values will vary from 2 to 12, with 7 being the most frequent sum and 2 and 12 being the least frequent sums.] Figure 7.32 shows the 36 possible combinations of the two dice. Your program should roll the two dice 36,000 times. Use a one-dimensional array to tally the numbers of times each possible sum appears. Print the results in a tabular format. Also, determine if the totals are reasonable (i.e., there are six ways to roll a 7, so approximately one-sixth of all the rolls should be 7).

CODE:

``````#include <iostream>
using std::cout;
using std::ios;

#include <iomanip>
using std::setw;
using std::setprecision;
using std::fixed;
using std::showpoint;

#include <cstdlib>
using std::rand;
using std::srand;

#include <ctime>
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 a 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 the 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 statement that iterates ROLL times. Randomly
generate values for x (i.e., die1) and (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\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"

return 0; // indicates successful completion
} // end main``````

## All 11 Replies

Well, it's so simple. You have integers in range 2..12. Declare an array of integers with size 13 (you may use indicies from 0 to 12). Initialize it with zeroes then add 1 to `array[sum]` element in the simulation main loop.
Use rand() with a proper scaling to get values in range 1..6 (see rand help, use RAND_MAX value).
That's all.
Of course, you are familiare with basic C++ declarations and statements...

Sum Total Expected Actual
2 1000 2.778% 2.778%
3 1958 5.556% 5.439%
4 3048 8.333% 8.467%
5 3979 11.111% 11.053%
6 5007 13.889% 13.908%
7 6087 16.667% 16.908%
8 4996 13.889% 13.878%
9 3971 11.111% 11.031%
10 2996 8.333% 8.322%
11 2008 5.556% 5.578%
12 950 2.778% 2.639%

Well, it's so simple. You have integers in range 2..12. Declare an array of integers with size 13 (you may use indicies from 0 to 12). Initialize it with zeroes then add 1 to `array[sum]` element in the simulation main loop.
Use rand() with a proper scaling to get values in range 1..6 (see rand help, use RAND_MAX value).
That's all.
Of course, you are familiare with basic C++ declarations and statements...

I think im getting confused when it says to /* Write a declaration of array exprected here. Assign an initializer list containing the expected values here. Use SIZE for the number of elements */

``int expected[ SIZE ] = { list containing expected values }``

I guess im not sure what the expected values are, I posted the sample output to the lab and it has the expected values, but there in percentages and im not sure if thats what im suppose to list.

So here's what I have so far, I know it's not correct but I think im on the right path, can someone let me know whats wrong, it's either the expected values in the list or it has something to do with the for roll statement

``````#include <iostream>
using std::cout;
using std::ios;

#include <iomanip>
using std::setw;
using std::setprecision;
using std::fixed;
using std::showpoint;

#include <cstdlib>
using std::rand;
using std::srand;

#include <ctime>
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
int expected[ SIZE ] = { 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1 };
int x; // first die
int y; // second die
int sum[ SIZE ] = { 0 };

srand( time( 0 ) );

// roll dice 36,000 times
for ( int roll = 0; roll < ROLLS; ++roll )
{
x = 1 + rand() % 6;
y = 1 + rand() % 6;
++sum[ x + y ];
}

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";

return 0; // indicates successful completion
} // end main``````

Please, start IDE, open your project, click on rand() call in the source then press sacramental key F1. Read about returned value range (RAND_MAX macros) then take a piece of paper and a pencil and perform a simple calculations: how to get a numbers from 1 to 6...

So here's what I have so far, I know it's not correct but I think im on the right path, can someone let me know whats wrong...

Actually, it's your job to tell us what's wrong, not let us try to figure out what the program is doing and what we think is wrong...

Please, start IDE, open your project, click on rand() call in the source then press sacramental key F1. Read about returned value range (RAND_MAX macros) then take a piece of paper and a pencil and perform a simple calculations: how to get a numbers from 1 to 6...

Maybe you need to explain what's wrong with his calculation. Seems to be fine to me.

I think im getting confused when it says to /* Write a declaration of array exprected here. Assign an initializer list containing the expected values here. Use SIZE for the number of elements */

``int expected[ SIZE ] = { list containing expected values }``

I guess im not sure what the expected values are, I posted the sample output to the lab and it has the expected values, but there in percentages and im not sure if thats what im suppose to list.

Expected value is a statistics term. In your case, if you roll the dice 36,000 times and you define X as the number of 7's you roll in those 36,000 trials, then E(X) = 6,000. E(X) is notation for "the expected number of times X occurs", or "expected value of X". That's derived by multiplying the probability of getting a 7 in one roll (which is 6 / 36, or 16.67%, since there are 6 ways to get a 7 and there are 36 ways to roll two dice) times 36,000. So you'll simulate 36,000 rolls and keep track of how often each score occurs, and it's unlikely you'll get exactly 6,000 sevens in 36,000 rolls, but hopefully you'll get close to that. It appears that the format you are supposed to display is in percentages, so you'll divide that number of 7's (and the other scores too) by 36,000 and display that number next to 16.67%. That number should be fairly close to 16.67%.

Now I think it's legal to use `1 + rand() % 6`(if you have a proof for uniform distribution of mod 6 ;)).
I'm accustomed to good pseudorandom generators with range 0..1...

Alright guys, I just figured it out right now, I had the expected array listed as SIZE which meant there were 13 saved slots, I was only putting in originally 11, so that's why I was missing two slots in my output. Then I realized that "int j" begins at 2, which means that the first two slots of expected array are 0, since you cant roll a zero or a one with two dice. And finally, I came to this

``````#include <iostream>
using std::cout;
using std::ios;

#include <iomanip>
using std::setw;
using std::setprecision;
using std::fixed;
using std::showpoint;

#include <cstdlib>
using std::rand;
using std::srand;

#include <ctime>
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
int expected[ SIZE ] = { 0, 0, 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1 };
int x; // first die
int y; // second die
int sum[ SIZE ] = { 0 };

srand( time( 0 ) );

// roll dice 36,000 times
for ( int roll = 0; roll < ROLLS; ++roll )
{
x = 1 + rand() % 6;
y = 1 + rand() % 6;
++sum[ x + y ];
}

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";

return 0; // indicates successful completion
} // end main``````

Aomething stupid? No. It's a common error. Don't forget: array and array are unused (no sum 0 and sum 1) in both arrays (expected and sum). So add two leading zeroes (or any other values) in initialization list for expected array - that's all.

thanks for the help!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.