Question: Write a program in C++ that simulates a sequence of tosses of a coin given the probability of heads double p (in one toss) and

Write a program in C++ that simulates a sequence of tosses of a coin given the probability of heads double p (in one toss) and the number of experiments int n; (see the explanation below).

Fair and unfair coins: If a fair coin is tossed N times, then the relative frequency of heads (the number of times heads appear in N tosses divided by the number of experiments N) approaches to the probability of heads p = 1/2 as N increases. In other words, heads come up in approximately a half of the tosses. The coin is called unfair if the probability of heads (in one toss) is p does not equal 1/2 .

How to simulate a toss of a coin? Suppose that the probability of heads p ? (0, 1) is given. Let bool y be a variable which will contain the result of the simulation; the value true will correspond to heads and false to tails. First generate a (uniformly distributed) random floating-point number x ? [0, 1] employing the function rand(). If x

Instructions:

Implement a function bool coin_toss(double p) that simulates a toss of a coin with the probability of heads double p provided as an input. The function must return true for heads and false for tails. To implement this function follow the algorithm described above.

Implement a function int head_frequency(double p, int n) that simulates a sequence of n tosses of a coin given the probability of heads p ? (0, 1). The function must return the number of times heads come up in n tosses. To implement this function employ the function coin toss that simulates one toss of a coin.

Write a program that prompts the user to enter the probability of heads double p and the number of experiments int n. Then simulate a sequence of n tosses of a coin using the function head frequency. The program must compute the relative frequency of heads and compare it with the probability p.

You can assume that the the users input is always valid.

Implement a loop in which the above actions are repeated until the user requests to quit.

Set the seed using srand(time(0)). Note, the results you obtain on your computer will differ from the output presented below, but the qualitative behavior will be the same.

See sample input-output below:

Write a program in C++ that simulates a sequence of tosses of

a coin given the probability of heads double p (in one toss)

C:Windowslsystem321cmd.exe Enter the probability of heads 0

y Enter the probability of heads 0Kp y Enter the probability of heads 0Kp 1: 0.75 Enter the nubmer of experiments: 1000000 "Heads" relative frequency f-0.750056 Distance If-p 0.000056 Continue? (y> y Enter the probability of heads Kp1 .75 Enter the nubmer of experiments: 100000000 "Heads" relative frequency:-0.750064 Distance: lf-p | =0-000064 Continue? (y> n Press any key to continue _

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!