Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*DIRECTIONS Write a program that simulates the rolling of two dice. Your program should roll the two dice 50,000 times.
- Use a one-dimensional array to tally the numbers of times each possible sum appears.
- Display the number of times each sum appears and its percentage out of the total numbers of rolls. In other words,
- calculate the probability of each sum using the law of large numbers.
- mathew myers
- Template blank Source document*/
- #include<iostream>
- #include<string>
- #include<sstream>
- #include<cmath>
- #include<stdlib.h>
- #include<time.h>
- //globals
- using namespace std;
- bool runAgain(void);
- const int DIESIZE = 6; //options options options... it would be nice to have this as a program perameter
- const int NUMOFDICE = 2;
- const int DIEMAX = DIESIZE * NUMOFDICE;
- const int DIEMIN = 1 * NUMOFDICE;
- const int ROLLS = 50000;
- int Roll();
- int a[DIEMAX]={0};
- void main()
- {
- //Declarations
- bool b = true;
- int temp = 0;
- srand (unsigned (time(NULL)));
- do{
- //inputs
- if (b)
- {
- cout<<"The probability of " << NUMOFDICE << " d"<<DIESIZE << " adding up to each possible sum will be calculated by rolling "<<ROLLS <<" times.\n";
- b = false;
- }
- for( int i = 0; i <= DIEMAX - DIEMIN;i++)
- {
- a[i] = 0;
- }
- for( int i = 0; i < ROLLS;i++)
- {
- a[Roll()]++;
- }
- //outputs
- for( int i = 0; i <= DIEMAX - DIEMIN;i++)
- {
- cout <<i+DIEMIN <<" is "<< a[i]<<"/"<<ROLLS<<" "<< (a[i]*100.0/ROLLS)<<"%\n" ;
- }
- }while( runAgain() );
- }
- int Roll()
- {
- int derp = 0;
- for( int i = 0; i < NUMOFDICE;i++)
- {
- derp = derp +(rand() % DIESIZE + 1);
- }
- return derp - DIEMIN;
- }
- bool runAgain(void){
- char userResponse;
- cout << "\nWould you like to run again (y or n): ";
- cin >> userResponse;
- if(userResponse == 'y')
- return(true);
- return(false);
- }
- /* lazy copy stuff
- if()
- {
- }
- else if()
- {
- }
- else
- {
- }
- for( int i = 0; i < x;i++)
- {
- }
- switch()
- {
- case :
- break;
- case :
- break;
- default:
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment