RSS Forums RSS
Please support our C++ advertiser: Programming Forums

Help with rolling dice problem

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  
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 12:57 pm.
Newsletter Archive - Sitemap - Privacy Statement - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC