For this problem you will create two files: diceFunctions.cpp and main.cpp. Heed
ID: 3544917 • Letter: F
Question
For this problem you will create two files: diceFunctions.cpp and main.cpp. Heed the following specifications. diceFunctions.cpp contains the following functions:
int randomInt(int low, int high) - randomInt() returns a random integer in the range low-high, inclusive.
double findOddsForDiceRolling(int sumBothFaces, int numTrials) - findOddsForDiceRolling() returns a number that is an estimate of the odds of throwing two dice whose numbers on the top add to sumBothFaces. The estimate is computed by calling randomInt() numTrials times for each of the two dice to simulate rolling them. main.cpp prompts the user for the number of trials that should be used for the simulation, then uses findOddsForDiceRolling() to estimate the odds of rolling numbers 1-12. It then produces output, formatted as shown below, giving the odds estimate for each sum and finally emitting the total of the odds. The decimal points of the odds and the total should line up, as shown below.
Here is an example run with input highlighted. Enter number of trials desired: 9999999
The estimated odds of rolling a 1 with 2 dice are 0
The estimated odds of rolling a 2 with 2 dice are 0.0277689
The estimated odds of rolling a 3 with 2 dice are 0.0556608
The estimated odds of rolling a 4 with 2 dice are 0.0832939
The estimated odds of rolling a 5 with 2 dice are 0.111123
The estimated odds of rolling a 6 with 2 dice are 0.138995
The estimated odds of rolling a 7 with 2 dice are 0.166651
The estimated odds of rolling a 8 with 2 dice are 0.138985
The estimated odds of rolling a 9 with 2 dice are 0.111044
The estimated odds of rolling a 10 with 2 dice are 0.0833574
The estimated odds of rolling a 11 with 2 dice are 0.055636
The estimated odds of rolling a 12 with 2 dice are 0.0277821 --------- Total 1.0003
NUMTRIALS
Explanation / Answer
#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
int randomInt(int low, int high)
{
return (rand()%(high-low+1)) + low;
}
double findOddsForDiceRolling(int sumBothFaces, int numTrials)
{
int sum = 0;
int count =0;
for(int i=0; i<numTrials; i++)
{
sum = randomInt(1,6)+ randomInt(1,6);
if(sum == sumBothFaces)
count++;
}
return static_cast<double> (count)/numTrials;
}
int main()
{
srand(time(NULL));
int numTrials;
cout <<"Enter number of trials desired: ";
cin >> numTrials;
cout << endl;
double total =0;
for(int i=1; i<=12; i++)
{
double local = findOddsForDiceRolling(i,numTrials);
cout << "The estimated odds of rolling a " << i << " with 2 dice are " << local << endl;
total = total + local;
}
cout <<"--------- Total "<<total<<" NUMTRIALS" << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.