Question: Design and test a C++ program to experimentally determine the probability (odds) of tossing 5 heads (of a coin) in a row. Allow the user
Design and test a C++ program to experimentally determine the probability (odds) of tossing 5 heads (of a coin) in a row. Allow the user to enter the number of trials. Each trial involves tossing a coin 5 times. (You will need a "for" or a "while" loop. It is your choice.)
The program output should be the percentage (%) that 5 heads occurred. Record the results for various numbers of trails (look for convergence). Compare to mathematical (theoretical) answer. Pick meaningful test cases and include all results and comments in the cpp file. Show at least 5 test cases. How many trials are required to get the program result to be within 1) 1% and 2) 0.1% of the theoretical answer?
Enter ntrials from user
Loop ntrials times
{
toss1 = rand() % 2;
toss2 = rand() % 2;
toss3 = rand() % 2;
toss4 = rand() % 2;toss5 = rand() % 2;
if (all 5 tosses are heads) counter++
}
Percentage of tossing 5 heads =100 * counter/ntrials (Hint: use cast operator)Compare to theoretical results (= 1/32); find and display % difference
make sure you set the number of trials high enough to show convergence to the theoretical value. You might have to iterate 50,000 or 100,000 times or more. Include a calculation for the % error = 100.0 * fabs( calculated % - theoretical % )/ theoretical %. Note: the theoretical result = 3.125% or 1/32)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
