View Single Post
Join Date: Jun 2008
Posts: 13
Reputation: cmatos15 is an unknown quantity at this point 
Solved Threads: 0
cmatos15 cmatos15 is offline Offline
Newbie Poster

Help with rolling dice problem

 
0
  #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
Reply With Quote