In: Computer Science
Write a program to simulate an experiment flipping three coins. Each time the three coins are flipped, is called a “trial”. A coin flip can randomly result in either “Heads” (1) or “Tails” (0) Allow the user to enter the number of “trial”s to simulate. Print out each coins’ result after each “trial”. Use seed value 1234. Tally up and determine what percentage of “trial”’s all three coins land on “Heads” in the simulation. C language
If you have any doubts, please give me comment...
#include<stdio.h>
#include<stdlib.h>
int main(){
srand(1234);
int n, i, result_coin1, result_coin2, result_coin3, heads = 0;
printf("enter the number of \"trial\"s to simulate: ");
scanf("%d", &n);
for(i=1; i<=n; i++){
result_coin1 = rand()%2;
result_coin2 = rand()%2;
result_coin3 = rand()%2;
heads = 0;
printf("Trial %d:\t", i);
if(result_coin1){
printf("Heads");
heads++;
}
else
printf("Tails");
printf("\t");
if(result_coin2){
printf("Heads");
heads++;
}
else
printf("Tails");
printf("\t");
if(result_coin3){
printf("Heads");
heads++;
}
else
printf("Tails");
printf("\tHeads(%%): %.2f\n", (heads/3.0)*100);
}
return 0;
}