KySoto

Untitled

Sep 23rd, 2013
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 KB | None | 0 0
  1. /*DIRECTIONS Write a program that simulates the rolling of two dice.  Your program should roll the two dice 50,000 times.  
  2.  
  3. Use a one-dimensional array to tally the numbers of times each possible sum appears.
  4.  
  5. Display the number of times each sum appears and its percentage out of the total numbers of rolls.  In other words,
  6. calculate the probability of each sum using the law of large numbers.
  7. mathew myers
  8. Template blank Source document*/
  9.  
  10. #include<iostream>
  11. #include<string>
  12. #include<sstream>
  13. #include<cmath>
  14. #include<stdlib.h>
  15. #include<time.h>
  16.  
  17. //globals
  18. using namespace std;
  19.  
  20. bool runAgain(void);
  21. const int DIESIZE = 6; //options options options... it would be nice to have this as a program perameter
  22. const int NUMOFDICE = 2;
  23. const int DIEMAX = DIESIZE * NUMOFDICE;
  24. const int DIEMIN = 1 * NUMOFDICE;
  25. const int ROLLS = 50000;
  26. int Roll();
  27. int a[DIEMAX]={0};
  28.  
  29.  
  30. void main()
  31. {
  32. //Declarations
  33.     bool b = true;
  34.     int temp = 0;
  35.     srand (unsigned (time(NULL)));
  36.     do{
  37. //inputs
  38.         if (b)
  39.         {
  40.             cout<<"The probability of " << NUMOFDICE << " d"<<DIESIZE << " adding up to each possible sum will be calculated by rolling "<<ROLLS <<" times.\n";
  41.             b = false;
  42.         }
  43.         for( int i = 0; i <= DIEMAX - DIEMIN;i++)
  44.         {
  45.             a[i] = 0;
  46.         }
  47.         for( int i = 0; i < ROLLS;i++)
  48.         {
  49.  
  50.             a[Roll()]++;
  51.         }
  52. //outputs
  53.         for( int i = 0; i <= DIEMAX - DIEMIN;i++)
  54.         {
  55.         cout <<i+DIEMIN <<" is "<< a[i]<<"/"<<ROLLS<<" "<< (a[i]*100.0/ROLLS)<<"%\n" ;
  56.         }
  57.     }while( runAgain() );
  58.  
  59. }
  60. int Roll()
  61. {
  62.     int derp = 0;
  63.     for( int i = 0; i < NUMOFDICE;i++)
  64.     {
  65.         derp = derp +(rand() % DIESIZE + 1);
  66.     }
  67.     return derp - DIEMIN;
  68. }
  69.  
  70. bool runAgain(void){
  71.     char userResponse;
  72.  
  73.     cout << "\nWould you like to run again (y or n): ";
  74.     cin >> userResponse;
  75.  
  76.     if(userResponse == 'y')
  77.         return(true);
  78.  
  79.     return(false);
  80. }
  81. /* lazy copy stuff
  82. if()
  83. {
  84. }
  85. else if()
  86. {
  87. }
  88. else
  89. {
  90. }
  91.  
  92. for( int i = 0; i < x;i++)
  93. {
  94. }
  95. switch()
  96. {
  97.     case :
  98.     break;
  99.     case :
  100.     break;
  101.     default:
  102. }
  103. */
Advertisement
Add Comment
Please, Sign In to add comment