4 Replies - 8463 Views - Last Post: 13 November 2007 - 12:59 PM Rate Topic: -----

#1 James Bond C++ Spy   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 132
  • Joined: 03-October 07

Roll 2 dice and keep up with the results using a array

Posted 08 November 2007 - 12:26 PM

Need to write a program to simulate the roll of 2 dice showing the result and the total of the two rolls. The dice should both be rolled 36,000 times. Use a on dim array to tally the number of times each possible sum appears. Print results in a tabular format. Also determine if the results are reasonable.

The output should be a table that has the following headings:
Sum of Die, # of Rolls, Actual %, and Expected %. The # of Rolls is the number of times the Sum was rolled. Actual % is the % of # of Rolls based on 36,000 rolls. Expected % is the calculated number of time you would expect that the Sum is rolled. I don't really get it. Here is my (nonworking) code so far. Thanks for any help. :)

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using std::cout;
using std::endl;
using std::rand;
using std::srand;
using std::time;

int main()
{
	  const int arraySize = 36;
	  int frequency[ arraySize ] = { 0 };
	  
	  srand( time( 0 ));
	  
	  for (int roll1 = 1; roll1 <= 36000; roll1++)
	  frequency[ 1 + rand() % 6 ]++;

	  for (int roll2 = 1; roll2 <= 36000; roll2++)
	  frequency[ 1 + rand() % 6 ]++;
	  
	  cout << "Sum of Die" << setw( 13 ) << "# of Rolls" << endl;
	  
	  for (int face1 = 1; face1 < arraySize; face1++)
	  cout << setw( 4 ) << face1 << setw( 13 ) << frequency[ face1 ] << endl;
	  
	  for (int face2 = 1; face2 < arraySize; face2++)
	  cout << setw( 4 ) << face2 << setw( 13 ) << frequency[ face2 ] << endl;

	  return 0;
 
} // end main


This post has been edited by James Bond C++ Spy: 08 November 2007 - 01:46 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Roll 2 dice and keep up with the results using a array

#2 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6968
  • View blog
  • Posts: 31,958
  • Joined: 10-May 07

Re: Roll 2 dice and keep up with the results using a array

Posted 08 November 2007 - 12:30 PM

View PostJames Bond C++ Spy, on 8 Nov, 2007 - 12:26 PM, said:

Need to write a program to simulate the roll of 2 dice showing the result and the total of the two rolls.

The dice should both be rolled 36,000 times.

Use a on dim array to tally the number of times each possible sum appears.

Print results in a tabular format.

Also determine if the results are reasonable.


Start off with a function that will throw random 1 through 6, twice (once for each die). Then add those two values.

Now run that function 36,000 times.

Tab the output.

Make a way to programatically double check the results. Make sure each result is less than six, plus less than six, equals less than 12.
Was This Post Helpful? 0
  • +
  • -

#3 James Bond C++ Spy   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 132
  • Joined: 03-October 07

Re: Roll 2 dice and keep up with the results using a array

Posted 08 November 2007 - 01:46 PM

Can someone please help me with my code. Thanks

This post has been edited by James Bond C++ Spy: 08 November 2007 - 01:47 PM

Was This Post Helpful? 0
  • +
  • -

#4 jjhaag   User is offline

  • me editor am smartastic
  • member icon

Reputation: 49
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Roll 2 dice and keep up with the results using a array

Posted 08 November 2007 - 02:55 PM

Well, setw is in the std namespace, so you'll need to take care of that as you have done for cout, etc.

You really only need a single loop for the output step - the point is to display the frequencies of the sum of the dice, not each value of each die individually. So add together the value of each of the dice for each roll. Then tally the results and display the frequencies.

If you're having more problems than that, you'll need to actually describe them in detail, and not just say that the code is not working. Post any compiler errors, sample output, how the output is different from what you're looking for, etc.
Was This Post Helpful? 0
  • +
  • -

#5 James Bond C++ Spy   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 132
  • Joined: 03-October 07

Re: Roll 2 dice and keep up with the results using a array

Posted 13 November 2007 - 12:59 PM

Thanks for the help but I have it now

Working Code:
#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



This post has been edited by James Bond C++ Spy: 13 November 2007 - 08:12 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1