Design and test a C++ program to experimentally determine the probability (odds)
ID: 3670902 • Letter: D
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 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
Program outline - pseudocode
Explanation / Answer
#include <iostream>
#include<cstdlib>
using namespace std;
int main()
{
int n,i,toss1,toss2,toss3,toss4,toss5,counter=0;
float expper,tsper;
cout<<"enter the trails:";
cin>>n;
for(i=0;i<n;i++)
{
toss1= rand()%2;
toss2= rand()%2;
toss3= rand()%2;
toss4= rand()%2;
toss5= rand()%2;
if(toss1==1&&toss2==1&&toss3==1&&toss4==1&&toss5==1)
{
counter++;
}
}
expper= 100*counter/n;
tsper=100*n/32;
cout<<"experimental result is: "<<expper<<"%";
cout<<"theoretical result is: "<<tsper<<"%";
if(expper == tsper)
{
cout<<"both the results are equal ";
}
else
{
cout<<"the experiment results and theoretical results are not equal ";
}
cout<<"difference between theoretical and experimental is "<<tsper-expper<<"%";
return 0;
}
number of trails 2:
output:
enter the trails:experimental result is:0%
theoretical result is:6%
the experiment results and theoretical results are not equal
difference between theoretical and experimental is 6%
number of trails 32:
enter the trails:experimental result is:
0%theoretical result is:
6%the experiment results and theoretical results are not equal
difference between theoretical and experimental is 6%
number of trails 35:
enter the trails:experimental result is:2%
theoretical result is:12%
the experiment results and theoretical results are not equal
difference between theoretical and experimental is 10%
number of trails 40:
enter the trails:experimental result is:2%
theoretical result is:9%
the experiment results and theoretical results are not equal
difference between theoretical and experimental is 7%
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.