Write a function named cointoss that simulates the tossing of a coin. When you c
ID: 663053 • Letter: W
Question
Write a function named cointoss that simulates the tossing of a coin. When you call the function, it should generate a random number in the range of 1 through 2. If the random number is 1, the function should display "heads". If the random number is 2, the function should display "tails". Demonstrate the function in a program that asks the user how many times the coin should be tossed, and then simulates tossing the coin that number of times. Report the total number of heads and tails. Write a function named cointoss that simulates the tossing of a coin. When you call the function, it should generate a random number in the range of 1 through 2. If the random number is 1, the function should display "heads". If the random number is 2, the function should display "tails". Demonstrate the function in a program that asks the user how many times the coin should be tossed, and then simulates tossing the coin that number of times. Report the total number of heads and tails. Write a function named cointoss that simulates the tossing of a coin. When you call the function, it should generate a random number in the range of 1 through 2. If the random number is 1, the function should display "heads". If the random number is 2, the function should display "tails". Demonstrate the function in a program that asks the user how many times the coin should be tossed, and then simulates tossing the coin that number of times. Report the total number of heads and tails.Explanation / Answer
IN C
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int coinToss(){
int diff = 2-1;
int r=(int) (((double)(diff+1)/RAND_MAX) * rand() + 1);
if(r==2 || r==1)
{
if(r == 1)
{
printf("HEADS!!!!!! ");
return 0;
}
if(r==2)
{
printf("TAILS!! ");
return 1;
}
}
}
int main(void) {
int n,i,count=0,k;
printf("how many times you want to flip the coin:");
scanf("%d",&n);
for(i=0;i<n;i++){
k= coinToss(count);
if(k==0){
count++;
}
else{
continue;
}
}
printf("Total Heads:%d ",count);
printf("Total Tails:%d ",(n-count));
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.