User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 426,015 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,675 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 510 | Replies: 11
Reply
Join Date: Jun 2008
Posts: 13
Reputation: cmatos15 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
cmatos15 cmatos15 is offline Offline
Newbie Poster

Help with rolling dice problem

  #1  
Jul 25th, 2008
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2008
Posts: 619
Reputation: ArkM is just really nice ArkM is just really nice ArkM is just really nice ArkM is just really nice 
Rep Power: 5
Solved Threads: 91
ArkM's Avatar
ArkM ArkM is offline Offline
Practically a Master Poster

Re: Help with rolling dice problem

  #2  
Jul 25th, 2008
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...
Reply With Quote  
Join Date: Jun 2008
Posts: 13
Reputation: cmatos15 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
cmatos15 cmatos15 is offline Offline
Newbie Poster

Re: Help with rolling dice problem

  #3  
Jul 25th, 2008
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%
Reply With Quote  
Join Date: Jun 2008
Posts: 13
Reputation: cmatos15 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
cmatos15 cmatos15 is offline Offline
Newbie Poster

Re: Help with rolling dice problem

  #4  
Jul 25th, 2008
Originally Posted by ArkM View Post
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 */

is that asking for
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.
Reply With Quote  
Join Date: Jun 2008
Posts: 13
Reputation: cmatos15 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
cmatos15 cmatos15 is offline Offline
Newbie Poster

Re: Help with rolling dice problem

  #5  
Jul 25th, 2008
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
Reply With Quote  
Join Date: Jul 2008
Posts: 619
Reputation: ArkM is just really nice ArkM is just really nice ArkM is just really nice ArkM is just really nice 
Rep Power: 5
Solved Threads: 91
ArkM's Avatar
ArkM ArkM is offline Offline
Practically a Master Poster

Re: Help with rolling dice problem

  #6  
Jul 25th, 2008
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...
Reply With Quote  
Join Date: May 2006
Posts: 2,723
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 15
Solved Threads: 222
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: Help with rolling dice problem

  #7  
Jul 26th, 2008
Originally Posted by cmatos15 View Post
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...

Originally Posted by ArkM View Post
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.
Age is unimportant -- except in cheese
Reply With Quote  
Join Date: Jan 2008
Posts: 1,771
Reputation: VernonDozier is just really nice VernonDozier is just really nice VernonDozier is just really nice VernonDozier is just really nice VernonDozier is just really nice 
Rep Power: 8
Solved Threads: 218
VernonDozier VernonDozier is offline Offline
Posting Virtuoso

Re: Help with rolling dice problem

  #8  
Jul 26th, 2008
Originally Posted by cmatos15 View Post
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 */

is that asking for
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%.
Reply With Quote  
Join Date: Jul 2008
Posts: 619
Reputation: ArkM is just really nice ArkM is just really nice ArkM is just really nice ArkM is just really nice 
Rep Power: 5
Solved Threads: 91
ArkM's Avatar
ArkM ArkM is offline Offline
Practically a Master Poster

Re: Help with rolling dice problem

  #9  
Jul 26th, 2008
Now I think it's legal to use [icode]1 + rand() % 6[/code] (if you have a proof for uniform distribution of mod 6 ).
I'm accustomed to good pseudorandom generators with range 0..1...
Reply With Quote  
Join Date: Jun 2008
Posts: 13
Reputation: cmatos15 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
cmatos15 cmatos15 is offline Offline
Newbie Poster

Re: Help with rolling dice problem

  #10  
Jul 26th, 2008
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
Last edited by cmatos15 : Jul 26th, 2008 at 2:19 am. Reason: n/a
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 1:16 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC