C Coin Toss Write a program to simulate the output of unbias

C++ Coin Toss

Write a program to simulate the output of unbiased coin tosses-equally likely head or tail. Your program should display the result of current toss (i.e. head or tail) and keep on tossing the coin and showing the output, until you get five straight heads. After you get five consecutive heads, your program should terminate and display how many tosses were required to get five straight heads. Now consider the case of a biased coin. The biased coin is likely to produce twice as many tails compared to heads. Solve Problem 1 (i) for this scenario.

Solution

Please find the required program along with its output. Please see the comments against each line to understand the step.

i)

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>

int toss( ) //for simulating the coin toss
{
if (rand() % 2 == 0) //if the random number generated is even, then consider toss if head, else a tail
return 0;
else
return 1;
}

int main()
{
int i=1;
int heads = 1;
int tails = 0;
bool prev = false;

srand(time(NULL)); //for random number generation to produce distinct value on each run

while(true){ //an infinite loop
  
int t = toss(); //toss the coin
printf( \"%d: %d\ \",i++, t); //print toss output
if (t == 0){ //if toss is a head
heads = 1;
while(true){ //then toss untill we get 5 head consequently
t = toss();
printf( \"%d: %d\ \",i, t);
i++;
if(t==0){ //if toss is head increment heads count
heads++;
}else
break; //else exit the inner while loop
  
if(heads>=5){ //if we encounter 5 heads in the inner loop, just exit from it
break;
}
}
if(heads>=5){ //exit from outer loop if heads count is 5
break;
}
}
}
printf( \"Total %d times flipped for getting 5 consecutive heads\ \", i); //print the result

return 0;

}

-------------------------

OUTPUT:

1: 1
2: 1
3: 0
4: 1
5: 0
6: 0
7: 0
8: 0
9: 1
10: 1
11: 0
12: 0
13: 0
14: 1
15: 0
16: 1
17: 0
18: 1
19: 1
20: 1
21: 0
22: 1
23: 1
24: 1
25: 0
26: 0
27: 0
28: 0
29: 0
Total 30 times flipped for getting 5 consecutive heads

II)

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>

int toss( ) //for simulating the coin toss
{
if (rand() % 3 == 0) //here random generation is taking a probabilty of 1/3 as beacuse the tail will be produced twice as compared to the heads.
return 0;
else
return 1;
}

int main()
{
int i=1;
int heads = 1;
int tails = 0;
bool prev = false;

srand(time(NULL)); //for random number generation to produce distinct value on each run

while(true){ //an infinite loop
  
int t = toss(); //toss the coin
printf( \"%d: %d\ \",i++, t); //print toss output
if (t == 0){ //if toss is a head
heads = 1;
while(true){ //then toss untill we get 5 head consequently
t = toss();
printf( \"%d: %d\ \",i, t);
i++;
if(t==0){ //if toss is head increment heads count
heads++;
}else
break; //else exit the inner while loop
  
if(heads>=5){ //if we encounter 5 heads in the inner loop, just exit from it
break;
}
}
if(heads>=5){ //exit from outer loop if heads count is 5
break;
}
}
}
printf( \"Total %d times flipped for getting 5 consecutive heads\ \", i); //print the result

return 0;

}

---------------------------

OUTPUT:

1: 0
2: 1
3: 1
4: 1
5: 1
6: 0
7: 1
8: 1
9: 1
10: 0
11: 0
12: 1
13: 0
14: 1
15: 1
16: 0
17: 0
18: 1
19: 0
20: 0
21: 1
22: 0
23: 1
24: 0
25: 1
26: 1
27: 1
28: 0
29: 1
30: 1
31: 1
32: 1
33: 1
34: 0
35: 1
36: 1
37: 1
38: 1
39: 0
40: 0
41: 1
42: 1
43: 0
44: 1
45: 1
46: 1
47: 0
48: 1
49: 1
50: 1
51: 0
52: 1
53: 1
54: 1
55: 0
56: 0
57: 1
58: 1
59: 1
60: 0
61: 1
62: 1
63: 1
64: 1
65: 1
66: 0
67: 0
68: 0
69: 1
70: 1
71: 0
72: 1
73: 1
74: 0
75: 1
76: 1
77: 0
78: 0
79: 1
80: 1
81: 1
82: 1
83: 0
84: 0
85: 1
86: 1
87: 1
88: 0
89: 1
90: 1
91: 1
92: 0
93: 1
94: 0
95: 1
96: 1
97: 1
98: 1
99: 1
100: 1
101: 0
102: 1
103: 1
104: 1
105: 1
106: 1
107: 1
108: 1
109: 0
110: 1
111: 0
112: 1
113: 1
114: 1
115: 0
116: 0
117: 1
118: 0
119: 1
120: 1
121: 1
122: 1
123: 1
124: 1
125: 1
126: 0
127: 0
128: 1
129: 1
130: 1
131: 0
132: 0
133: 0
134: 1
135: 1
136: 1
137: 1
138: 1
139: 1
140: 1
141: 1
142: 0
143: 1
144: 1
145: 1
146: 1
147: 1
148: 1
149: 1
150: 1
151: 1
152: 1
153: 0
154: 1
155: 0
156: 1
157: 1
158: 1
159: 1
160: 0
161: 1
162: 1
163: 1
164: 1
165: 1
166: 1
167: 1
168: 1
169: 1
170: 0
171: 0
172: 0
173: 1
174: 1
175: 1
176: 1
177: 1
178: 0
179: 1
180: 1
181: 1
182: 1
183: 1
184: 0
185: 0
186: 1
187: 1
188: 1
189: 1
190: 1
191: 1
192: 1
193: 1
194: 0
195: 0
196: 1
197: 1
198: 1
199: 1
200: 1
201: 0
202: 0
203: 0
204: 1
205: 1
206: 0
207: 1
208: 1
209: 0
210: 1
211: 1
212: 1
213: 1
214: 0
215: 1
216: 0
217: 0
218: 0
219: 0
220: 0
Total 221 times flipped for getting 5 consecutive heads

C++ Coin Toss Write a program to simulate the output of unbiased coin tosses-equally likely head or tail. Your program should display the result of current toss
C++ Coin Toss Write a program to simulate the output of unbiased coin tosses-equally likely head or tail. Your program should display the result of current toss
C++ Coin Toss Write a program to simulate the output of unbiased coin tosses-equally likely head or tail. Your program should display the result of current toss
C++ Coin Toss Write a program to simulate the output of unbiased coin tosses-equally likely head or tail. Your program should display the result of current toss
C++ Coin Toss Write a program to simulate the output of unbiased coin tosses-equally likely head or tail. Your program should display the result of current toss
C++ Coin Toss Write a program to simulate the output of unbiased coin tosses-equally likely head or tail. Your program should display the result of current toss
C++ Coin Toss Write a program to simulate the output of unbiased coin tosses-equally likely head or tail. Your program should display the result of current toss

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site