Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PLEASE provide the necessary missing code in the code below so that the sample p

ID: 3884932 • Letter: P

Question

PLEASE provide the necessary missing code in the code below so that the sample program dialog looks like that in "Sample Program Dialog". Please provide a screenshot with it compiled.

A Monte Carlo simulation attempts to demonstrate the approximate solution to a mathematical problem using random sampling techniques. For example, when one “throws” a coin N times and counts the number-of-heads, H, and the number-of-tails, T, the fact the quotient H/T approximates 1.0 empirically demonstrates probabilities associated with tossing a fair, 2-sided coin, namely, one expects H @ T @ N/2.

Problem

Allow the user to specify the number-of-sides of a die, S, the number-of-dice, D, and the number-of-trials, T. Because there is no requirement to validate user input, you may safely assume

(1 £ S £ 11), (1 £ D £ 11), and (T ³ 1)

By definition, a trial consists of repeatedly throwing the D Ssided dice until all the dice thrown show exactly the same face. For example, for S = 6 and D = 2, 1 trial might require the 8 throws (1,6) (2,3) (1,4) (5,6) (2,5) (4,6) (3,4) and finally (2,2).

After all trials have been run, display 1 line which shows the minimum number-of-throws used for a trial, the maximum number-of-throws used for a trial, and the average of the number-of-throws used for all trials using the format shown below. Note#1 The average is the real number which is computed as the quotient of the sum of number-of-throws for all trials (numerator) and the number-of-trials (denominator).

         111111111122222222223333333333444444444455555555

123456789012345678901234567890123456789012345678901234567

Minimum XXXXXXXXXX, maximum XXXXXXXXXX, average XXXXXXXX.X

Note #2 Your Monte Carlo simulation is attempting to show the average number-of-throws required for D S-sided dice is SD/S = SD-1, the number predicted by the theory of the geometric distribution which is implicit in the dice-throwing experiment.

//------------------------------------------------------

// Dr. Smith

// Practical Examination Problem

// PE57.cpp

//------------------------------------------------------

#include <iostream>

#include <iomanip>

#include <cstdlib>

#include <ctime>

using namespace std;

//--------------------------------------------------

void SetRandomSeed(void)

//--------------------------------------------------

{

   srand( (unsigned int) time(NULL) );

}

//--------------------------------------------------

double RandomReal(void)

//--------------------------------------------------

{

   double r;

   int i;

   i = rand();

// i is in [ 0,RAND_MAX ]

   if ( i == 0 )

      r = 0.0;

   else

      r = (double) (i-1)/RAND_MAX;

// r is in [ 0.0,1.0 )

   return( r );

}

//--------------------------------------------------

int RandomInteger(int LB,int UB)

//--------------------------------------------------

{

   double RandomReal(void);

   return( (int) (RandomReal()*(UB-LB+1)) + LB );

}

//--------------------------------------------------

bool RandomBoolean(double bias)

//--------------------------------------------------

{

   double RandomReal();

   return( RandomReal() <= bias );

}

//--------------------------------------------------

char RandomCharacter(char characters[],int size)

//--------------------------------------------------

{

   int RandomInteger(int LB,int UB);

   return( characters[RandomInteger(0,(size-1))] );

}

//--------------------------------------------------

int main()

//--------------------------------------------------

{

   void RunOneTrial(int S,int D,int &throws);

   int S,D;

   int T,minimum,maximum,throws,sum;

   cout << "S? "; cin >> S;

   cout << "D? "; cin >> D;

   cout << "T? "; cin >> T;

   SetRandomSeed();

   Student provides missing code to ²run² T trials to determine and subsequently display

      the minimum, maximum, and average = sum/T of number-of-throws required for the T trials.

   system("PAUSE");

   return( 0 );

}

//--------------------------------------------------

void RunOneTrial(int S,int D,int &throws)

//--------------------------------------------------

{

   bool AllDieTheSame(int die[],int D);

   int *die = new int [ D ];

   Student provides missing code to ²run² one trial, counting the number-of-throws

      of the D S-sided dice required before all die faces are the same.

   delete [] die;

}

//--------------------------------------------------

bool AllDieTheSame(int die[],int D)

//--------------------------------------------------

{

   Student provides missing code to implement the quantification

      AllDieTheSame = "i (die[i] = die[i+1]), i Î [ 0,D-2 ]

   ...or equivalently

    AllDieTheSame = (die[0] = die[1]) && (die[1] = die[2]) && ... && (die[D-2] = die[D-1])

}

Sample Program Dialog S? 6 D? 5 T? 10000 Minimum L, maximum 11784, average 1290.2 S? 10 D? 2 T? 10000 Minimum (6) 5-1 = 1296 1, maximum 96, average 9.9 (10)2-1 = 10

Explanation / Answer

#include <cstdlib>

#include <ctime>

using namespace std;

//--------------------------------------------------

void SetRandomSeed(void)

//--------------------------------------------------

{

   srand( (unsigned int) time(NULL) );

}

//--------------------------------------------------

double RandomReal(void)

//--------------------------------------------------

{

   double r;

   int i;

   i = rand();

// i is in [ 0,RAND_MAX ]

   if ( i == 0 )

      r = 0.0;

   else

      r = (double) (i-1)/RAND_MAX;

// r is in [ 0.0,1.0 )

   return( r );

}

//--------------------------------------------------

int RandomInteger(int LB,int UB)

//--------------------------------------------------

{

   double RandomReal(void);

   return( (int) (RandomReal()*(UB-LB+1)) + LB );

}

//--------------------------------------------------

bool RandomBoolean(double bias)

//--------------------------------------------------

{

   double RandomReal();

   return( RandomReal() <= bias );

}

//--------------------------------------------------

char RandomCharacter(char characters[],int size)

//--------------------------------------------------

{

   int RandomInteger(int LB,int UB);

   return( characters[RandomInteger(0,(size-1))] );

}

//--------------------------------------------------

int main()

//--------------------------------------------------

{

   void RunOneTrial(int S,int D,int &throws);

   int S,D;

   int T,minimum,maximum,throws,sum;

   cout << "S? "; cin >> S;

   cout << "D? "; cin >> D;

   cout << "T? "; cin >> T;

   SetRandomSeed();

   Student provides missing code to ²run² T trials to determine and subsequently display

      the minimum, maximum, and average = sum/T of number-of-throws required for the T trials.

   system("PAUSE");

   return( 0 );

}

//--------------------------------------------------

void RunOneTrial(int S,int D,int &throws)

//--------------------------------------------------

{

   bool AllDieTheSame(int die[],int D);

   int *die = new int [ D ];

   Student provides missing code to ²run² one trial, counting the number-of-throws

      of the D S-sided dice required before all die faces are the same.

   delete [] die;

}

//--------------------------------------------------

bool AllDieTheSame(int die[],int D)

//--------------------------------------------------

{

   Student provides missing code to implement the quantification

      AllDieTheSame = "i (die[i] = die[i+1]), i Î [ 0,D-2 ]

   ...or equivalently

    AllDieTheSame = (die[0] = die[1]) && (die[1] = die[2]) && ... && (die[D-2] = die[D-1])

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote