Question

In: Computer Science

Write a program that simulates flipping a coin repeatedly until three consecutive        heads are...

Write a program that simulates flipping a coin repeatedly until three consecutive
       heads are tossed. The program should then display the total number of times the coin was
       flipped. The user does not have to enter any information and we must use a while loop.

Solutions

Expert Solution

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

int main(int argc, char const *argv[])
{
   int totalFlip = 0, matchCount = 0;
   int previousNumber = -2, currentNumber = -1;
   // to reset random generation
   srand(time(0));
   printf("Number Generated: ");
   while(matchCount != 3)
   {  
       // temp will either be 0 or 1
       currentNumber = rand() % 2;
       printf("%d ",currentNumber);
       // check if match found
       if (currentNumber == previousNumber)
       {
           matchCount += 1;
       }
       // if mathch not found
       else
       {
           previousNumber = currentNumber;
           matchCount = 1;
       }
       totalFlip += 1;
   }
   printf("\ntotalFlip: %d\n", totalFlip);
   return 0;
}


Related Solutions

A coin is flipped repeatedly until either two heads appear in a row or two tails...
A coin is flipped repeatedly until either two heads appear in a row or two tails appear in a row(and then stop). Find the exact answer for P(two heads in a row appears before two tails in a row) for a coin with probability p of getting heads.
A fair coin is tossed repeatedly until it has landed Heads at least once and has...
A fair coin is tossed repeatedly until it has landed Heads at least once and has landed Tails at least once. Find the expected number of tosses.
In C++  Write a program that simulates coin tossing. For each toss of the coin the program...
In C++  Write a program that simulates coin tossing. For each toss of the coin the program should print heads or tails. Let the program toss the coin 100 times and count the number times each side of the coin appears. Print the results. 0 represents tails and 1 for heads.
A coin is tossed repeatedly until heads has occurred twice or tails has occurred twice, whichever...
A coin is tossed repeatedly until heads has occurred twice or tails has occurred twice, whichever comes first. Let X be the number of times the coin is tossed. Find: a. E(X). b. Var(X).
Write an application that simulates coin tossing. Let the program toss a coin each time the...
Write an application that simulates coin tossing. Let the program toss a coin each time the user chooses the “Toss Coin” menu option. Count the number times each side of the coin appears. Display the results. The program should call a method flip( ) that takes no arguments and returns a zero to represent a tail or a one to represent a head. There is a Random class that will allow you to generate a random integer. Import it from...
I have a coin and I know that the probability p of flipping heads is either...
I have a coin and I know that the probability p of flipping heads is either 1/2 or 1/4. I flip the coin 3 times and I get THT. Use the maximum likelihood method to determine the actual value of p.
- Your friend claims he has a fair coin; that is, the probability of flipping heads...
- Your friend claims he has a fair coin; that is, the probability of flipping heads or tails is equal to 0.5. You believe the coin is weighted. Suppose a coin toss turns up 15 heads out of 20 trials. At α = 0.05, can we conclude that the coin is fair (i.e., the probability of flipping heads is 0.5)? You may use the traditional method or P-value method.
Coin toss experiment In this experiment, determine the number of heads and tails after flipping a...
Coin toss experiment In this experiment, determine the number of heads and tails after flipping a coin for 1000 times. Use two different methods to find number of heads and tails Use for loops Use vectors in MATLAB. Repeat this experiment to find running average of flipping a coin for 200 and 2000 times. Plot the running average for two experiments using subplot functions
Coin toss experiment In this experiment, determine the number of heads and tails after flipping a...
Coin toss experiment In this experiment, determine the number of heads and tails after flipping a coin for 1000 times. Use two different methods to find number of heads and tails: Use for loops. Use vectors in MATLAB. Repeat this experiment to find running average of flipping a coin for 200 and 2000 times. Plot the running average for two experiments using subplot functions.
let's think about flipping a coin and gettings lots of heads in a row. What threshold...
let's think about flipping a coin and gettings lots of heads in a row. What threshold would you use for deciding the coin was not fair? What probability of getting some number of heads in a row would you use to decide the coin was baised? How many heads in a row would you need to get to reach your probability threshold?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT