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

Re: Help with rolling dice problem

 
0
  #10
Jul 26th, 2008
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

  1. #include <iostream>
  2. using std::cout;
  3. using std::ios;
  4.  
  5. #include <iomanip>
  6. using std::setw;
  7. using std::setprecision;
  8. using std::fixed;
  9. using std::showpoint;
  10.  
  11. #include <cstdlib>
  12. using std::rand;
  13. using std::srand;
  14.  
  15. #include <ctime>
  16. using std::time;
  17.  
  18. int main()
  19. {
  20. const long ROLLS = 36000;
  21. const int SIZE = 13;
  22.  
  23. // array expected contains counts for the expected
  24. // number of times each sum occurs in 36 rolls of the dice
  25. int expected[ SIZE ] = { 0, 0, 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1 };
  26. int x; // first die
  27. int y; // second die
  28. int sum[ SIZE ] = { 0 };
  29.  
  30. srand( time( 0 ) );
  31.  
  32. // roll dice 36,000 times
  33. for ( int roll = 0; roll < ROLLS; ++roll )
  34. {
  35. x = 1 + rand() % 6;
  36. y = 1 + rand() % 6;
  37. ++sum[ x + y ];
  38. }
  39.  
  40. cout << setw( 10 ) << "Sum" << setw( 10 ) << "Total" << setw( 10 )
  41. << "Expected" << setw( 10 ) << "Actual\n" << fixed << showpoint;
  42.  
  43.  
  44. // display results of rolling dice
  45. for ( int j = 2; j < SIZE; j++ )
  46. cout << setw( 10 ) << j << setw( 10 ) << sum[ j ]
  47. << setprecision( 3 ) << setw( 9 )
  48. << 100.0 * expected[ j ] / 36 << "%" << setprecision( 3 )
  49. << setw( 9 ) << 100.0 * sum[ j ] / 36000 << "%\n";
  50.  
  51.  
  52. return 0; // indicates successful completion
  53. } // end main
Last edited by cmatos15; Jul 26th, 2008 at 3:19 am. Reason: n/a
Reply With Quote