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
  #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

  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 ] = { 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
Reply With Quote