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

Design and test a C++ program to experimentally determine the probability (odds)

ID: 3528467 • 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 "while" loop.) 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 is required to get the program result to be within 1% of the theoretical answer. Enter ntrials from user Loop ntrials times

Explanation / Answer

//Given below is the code

#include <iostream>

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

using namespace std;

int main(){

srand(time(NULL));

int t;

cout<<"Enter the number of trials: ";

cin>>t;

int count;

for(int i=0;i<t;i++){

//Each loop is one trial

if(rand()%2==1){

if(rand()%2==1){

if(rand()%2==1){

if(rand()%2==1){

if(rand()%2==1){

count++;

}

}

}

}

}

}

cout<<"Percentage - "<<count*100/(t+0.0)<<" ";

}




trails


100 - 58


1000 - 8.3


10000 - 3.67


100000 - 3.19


1000000 - 3.1398


10000000 - 3.13551


100000000 - 3.13497


Theoriticlly the value should be 1/32 * 100 = 3.125


So the value seems to be approaching the theoritical value.


To get it to 1% range of 3.125 which is i.e 3.15625 , no of trails is approximately - 200000 to 300000

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