Write a program that simulates the rolling of two dice. The program should call rand to
roll the first die, and should call rand again to roll the second die. The sum of the two values
should then be calculated. [Note: Because each die has an integer value from 1 to 6,
then 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 below shows the 36 possible combinations
of the two dice.] Your program should roll the two dice 36,000 times. Use a singlesubscripted
array to tally the numbers of times each 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.
36 Possible Combinations
1 2 3 4 5 6
1 2 3 4 5 6 7
2 3 4 5 6 7 8
3 4 5 6 7 8 9
4 5 6 7 8 9 10
5 6 7 8 9 10 11
6 7 8 9 10 11 12
THIS IS WHAT I HAVE DONE SO FAR
#include <iostream>
using std::cout;
using std::fixed;
#include <iomanip>
using std::setprecision;
using std::setw;
#include <cstdlib>
#include <ctime>
int main()
{
const long ROLLS = 36000;
const int SIZE = 13;
//
//
//
//
//
//
int die1;
int die2;
srand( time( 0 ) );
//
//
//
cout << setw( 10 ) << "Sum" << setw( 10 ) << "Total"
<< setw( 10 ) << "Expected" << setw( 10 ) << "Actual\n"
<< fixed << setprecision( 3 );
for ( int j = 2; j < SIZE; ++j )
cout << setw( 10 ) << j << setw( 10 ) << sum[ j ]
<< setw( 9 ) << 100.0 * expected[ j ] / 36 << "%"
<< setw( 9 ) << 100.0 * sum[ j ] / 36000 << "%\n";
return 0;
} // end main
DICE PROGRAM, USING RANDTO ADD AND DISPLAY THE RESULTS
Page 1 of 1
5 Replies - 12398 Views - Last Post: 29 November 2007 - 08:11 PM
Replies To: DICE PROGRAM, USING RAND
#2
Re: DICE PROGRAM, USING RAND
Posted 28 November 2007 - 04:45 PM
We're not here to finish off your homework for you. Have you got a specific question?
#3
Re: DICE PROGRAM, USING RAND
Posted 28 November 2007 - 04:55 PM
Bench, on 28 Nov, 2007 - 04:45 PM, said:
We're not here to finish off your homework for you. Have you got a specific question?
I did'nt ment to say that. I was just wondering, what type of arrays shall I use, and how about finishing this off. Just need a hint, and I'll try. Its a bit weird, cause I have to do it 3600 times and also for 2 dies.
thanks
f26434
#4
Re: DICE PROGRAM, USING RAND
Posted 28 November 2007 - 05:38 PM
When you need to perform an instruction multiple times, then you need a statement capable of repetition (known as a loop). C++ supports at least 5 different ways of looping, although, at the moment, you only need one. If you haven't covered repetition yet in your course, have a look at the for and while keywords.
Rolling 2 die should be as simple as adding the result of 2 separate calls to the rand() function. (at least, this would be the preferred way to do it, in order to get the typical distribution). Write a program for a single pair of dice first, and build on that. Once you have a program which can roll one pair, try two pairs or three pairs. The difference between 3 pairs and 36000 pairs is a single number in your loop.
Your problem suggests that your die rolls have outcomes between 2 and 12 inclusive. Therefore, I'd suggest that your arrays need to have one element for each different outcome. You can use the array to keep a tally of the results (One way to do that would be to make an array large enough so that you could use indexes from 2 to 12).
Rolling 2 die should be as simple as adding the result of 2 separate calls to the rand() function. (at least, this would be the preferred way to do it, in order to get the typical distribution). Write a program for a single pair of dice first, and build on that. Once you have a program which can roll one pair, try two pairs or three pairs. The difference between 3 pairs and 36000 pairs is a single number in your loop.
Your problem suggests that your die rolls have outcomes between 2 and 12 inclusive. Therefore, I'd suggest that your arrays need to have one element for each different outcome. You can use the array to keep a tally of the results (One way to do that would be to make an array large enough so that you could use indexes from 2 to 12).
This post has been edited by Bench: 28 November 2007 - 05:40 PM
#5
Re: DICE PROGRAM, USING RAND
Posted 29 November 2007 - 08:25 AM
#include <iostream>
using std::cout;
using std::cin;
using std::fixed;
#include <iomanip>
using std::setprecision;
using std::setw;
#include <cstdlib>
#include<ctime>
int main()
{
const long ROLLS = 36000;
const int SIZE = 13;
int sum[SIZE] = {0};
int expected[SIZE] = {0,0,1,2,3,4,5,6,5,4,3,2,1};
int die1;
int die2;
srand(time(0));
for (int i=1; i <=ROLLS; i++)
{
die1 = rand()%6+1;
die2 = rand()%6+1;
sum[die1+die2]++;
}
cout<<setw(10)<<"Sum"<<setw(10)<<"Total"
<<setw(10)<<"Expected"<<setw(10)<<"Actual\n"
<<fixed<<setprecision(3);
for (int j =2; j<SIZE; j++)
cout <<setw(10)<<j<<setw(10)<<sum[j]
<<setw(9)<<100.0*expected[j]/36<<"%"
<<setw(9)<<100.0*sum[j]/36000<<"%\n";
return 0;
}
YES I HAVE DONE IT...... IT WORKS
using std::cout;
using std::cin;
using std::fixed;
#include <iomanip>
using std::setprecision;
using std::setw;
#include <cstdlib>
#include<ctime>
int main()
{
const long ROLLS = 36000;
const int SIZE = 13;
int sum[SIZE] = {0};
int expected[SIZE] = {0,0,1,2,3,4,5,6,5,4,3,2,1};
int die1;
int die2;
srand(time(0));
for (int i=1; i <=ROLLS; i++)
{
die1 = rand()%6+1;
die2 = rand()%6+1;
sum[die1+die2]++;
}
cout<<setw(10)<<"Sum"<<setw(10)<<"Total"
<<setw(10)<<"Expected"<<setw(10)<<"Actual\n"
<<fixed<<setprecision(3);
for (int j =2; j<SIZE; j++)
cout <<setw(10)<<j<<setw(10)<<sum[j]
<<setw(9)<<100.0*expected[j]/36<<"%"
<<setw(9)<<100.0*sum[j]/36000<<"%\n";
return 0;
}
YES I HAVE DONE IT...... IT WORKS
#6
Re: DICE PROGRAM, USING RAND
Posted 29 November 2007 - 08:11 PM
Check this out I had to do the same problem. Heres how I did it.
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using std::cout;
using std::endl;
using std::rand;
using std::srand;
using std::time;
using std::setw;
int main()
{
// Defining the arrays
const int arraySize = 13;
double frequency[ arraySize ] = { 0 };
double expected[ arraySize ] = {0, 0, 2.8, 5.6, 8.3, 11.1, 13.9, 16.7, 13.9, 11.1, 8.3, 5.6, 2.8};
srand( time( 0 ) );
// Rolling the die twice and summing the results in the array frenquency
for (int roll = 1; roll <= 36000; roll++)
frequency[ (1 + rand() % 6) + (1 + rand() % 6) ]++;
// Outputting the results of the die rolls (sum, frequency, actual percent and expected percent
cout << "Sum of Die" << setw( 20 ) << "Number of Rolls" << setw( 20 ) << "Actual Percent"
<< setw( 20 ) << "Expected Percent" << endl;
for (int face = 2; face < arraySize; face++)
cout << setw( 10 ) << face << setw( 20 ) << frequency[ face ] << setw( 20 )
<< ( frequency[ face ] / 36000 ) * 100 << setw( 20 ) << expected[ face ] << endl;
return 0;
} // end main
Page 1 of 1

New Topic/Question
Reply



MultiQuote


|