| | |
Help with rolling dice problem
![]() |
•
•
Join Date: Jun 2008
Posts: 13
Reputation:
Solved Threads: 0
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:
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 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
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...
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...
•
•
Join Date: Jun 2008
Posts: 13
Reputation:
Solved Threads: 0
•
•
•
•
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 toarray[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...
is that asking for
C++ Syntax (Toggle Plain Text)
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.
•
•
Join Date: Jun 2008
Posts: 13
Reputation:
Solved Threads: 0
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)
#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
•
•
•
•
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...
Maybe you need to explain what's wrong with his calculation. Seems to be fine to me.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Jan 2008
Posts: 3,750
Reputation:
Solved Threads: 491
•
•
•
•
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 forC++ Syntax (Toggle Plain Text)
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%.
•
•
Join Date: Jun 2008
Posts: 13
Reputation:
Solved Threads: 0
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)
#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 3:19 am. Reason: n/a
![]() |
Similar Threads
- Expression same but different results why (C++)
- C++ Random Numbers (C++)
- Program Help (C++)
- Clarity... (Java)
- Problem with numbers (Python)
- code help (C++)
Other Threads in the C++ Forum
- Previous Thread: another c++ problem: doesnt show messages
- Next Thread: Need help for a project
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ char class classes classified code coding compatible compile console conversion count date delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file filewrite forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper homeworksolutions iamthwee icon if...else ifstream input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node object output play pointer problem program programming project python random read recursion reference rpg string strings struct symbol temperature template test text text-file toolkit tree url values variable vector video win32 windows winsock wordfrequency wxwidgets







).