943,865 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 6461
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 25th, 2008
0

Help with rolling dice problem

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cmatos15 is offline Offline
13 posts
since Jun 2008
Jul 25th, 2008
0

Re: Help with rolling dice problem

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...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Jul 25th, 2008
0

Re: Help with rolling dice problem

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%
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cmatos15 is offline Offline
13 posts
since Jun 2008
Jul 25th, 2008
0

Re: Help with rolling dice problem

Click to Expand / Collapse  Quote originally posted by ArkM ...
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
C++ Syntax (Toggle Plain Text)
  1. 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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cmatos15 is offline Offline
13 posts
since Jun 2008
Jul 25th, 2008
0

Re: Help with rolling dice problem

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

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using std::cout;
  3. using std::ios;
  4.  
  5. #include <iomanip>
  6. using std::setw;
  7. using std::setprecision;
  8. using std::fixed;
  9. using std::showpoint;
  10.  
  11. #include <cstdlib>
  12. using std::rand;
  13. using std::srand;
  14.  
  15. #include <ctime>
  16. using std::time;
  17.  
  18. int main()
  19. {
  20. const long ROLLS = 36000;
  21. const int SIZE = 13;
  22.  
  23. // array expected contains counts for the expected
  24. // number of times each sum occurs in 36 rolls of the dice
  25. int expected[ SIZE ] = { 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1 };
  26. int x; // first die
  27. int y; // second die
  28. int sum[ SIZE ] = { 0 };
  29.  
  30. srand( time( 0 ) );
  31.  
  32. // roll dice 36,000 times
  33. for ( int roll = 0; roll < ROLLS; ++roll )
  34. {
  35. x = 1 + rand() % 6;
  36. y = 1 + rand() % 6;
  37. ++sum[ x + y ];
  38. }
  39.  
  40. cout << setw( 10 ) << "Sum" << setw( 10 ) << "Total" << setw( 10 )
  41. << "Expected" << setw( 10 ) << "Actual\n" << fixed << showpoint;
  42.  
  43.  
  44. // display results of rolling dice
  45. for ( int j = 2; j < SIZE; j++ )
  46. cout << setw( 10 ) << j << setw( 10 ) << sum[ j ]
  47. << setprecision( 3 ) << setw( 9 )
  48. << 100.0 * expected[ j ] / 36 << "%" << setprecision( 3 )
  49. << setw( 9 ) << 100.0 * sum[ j ] / 36000 << "%\n";
  50.  
  51.  
  52. return 0; // indicates successful completion
  53. } // end main
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cmatos15 is offline Offline
13 posts
since Jun 2008
Jul 25th, 2008
0

Re: Help with rolling dice problem

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...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Jul 26th, 2008
0

Re: Help with rolling dice problem

Click to Expand / Collapse  Quote originally posted by cmatos15 ...
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...

Click to Expand / Collapse  Quote originally posted by ArkM ...
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.
Moderator
Reputation Points: 3278
Solved Threads: 892
Posting Sage
WaltP is offline Offline
7,718 posts
since May 2006
Jul 26th, 2008
0

Re: Help with rolling dice problem

Click to Expand / Collapse  Quote originally posted by cmatos15 ...
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
C++ Syntax (Toggle Plain Text)
  1. 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%.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Jul 26th, 2008
0

Re: Help with rolling dice problem

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...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Jul 26th, 2008
0

Re: Help with rolling dice problem

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

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using std::cout;
  3. using std::ios;
  4.  
  5. #include <iomanip>
  6. using std::setw;
  7. using std::setprecision;
  8. using std::fixed;
  9. using std::showpoint;
  10.  
  11. #include <cstdlib>
  12. using std::rand;
  13. using std::srand;
  14.  
  15. #include <ctime>
  16. using std::time;
  17.  
  18. int main()
  19. {
  20. const long ROLLS = 36000;
  21. const int SIZE = 13;
  22.  
  23. // array expected contains counts for the expected
  24. // number of times each sum occurs in 36 rolls of the dice
  25. int expected[ SIZE ] = { 0, 0, 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1 };
  26. int x; // first die
  27. int y; // second die
  28. int sum[ SIZE ] = { 0 };
  29.  
  30. srand( time( 0 ) );
  31.  
  32. // roll dice 36,000 times
  33. for ( int roll = 0; roll < ROLLS; ++roll )
  34. {
  35. x = 1 + rand() % 6;
  36. y = 1 + rand() % 6;
  37. ++sum[ x + y ];
  38. }
  39.  
  40. cout << setw( 10 ) << "Sum" << setw( 10 ) << "Total" << setw( 10 )
  41. << "Expected" << setw( 10 ) << "Actual\n" << fixed << showpoint;
  42.  
  43.  
  44. // display results of rolling dice
  45. for ( int j = 2; j < SIZE; j++ )
  46. cout << setw( 10 ) << j << setw( 10 ) << sum[ j ]
  47. << setprecision( 3 ) << setw( 9 )
  48. << 100.0 * expected[ j ] / 36 << "%" << setprecision( 3 )
  49. << setw( 9 ) << 100.0 * sum[ j ] / 36000 << "%\n";
  50.  
  51.  
  52. return 0; // indicates successful completion
  53. } // end main
Last edited by cmatos15; Jul 26th, 2008 at 3:19 am. Reason: n/a
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cmatos15 is offline Offline
13 posts
since Jun 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: another c++ problem: doesnt show messages
Next Thread in C++ Forum Timeline: Need help for a project





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC