Question

Simulate the flipping of a coin Print the result of the coin flip : use off...

Simulate the flipping of a coin
Print the result of the coin flip : use off numbers for Heads . and even numbers for Tails
Update the number of times the result is Heads
Update the number of times the coin has been flipped

struct coin_prob{
int heads;
int flips;
};
void coin_flip(struct coin_prob * coin);
You may use a rand functioon (assume its already been seeded), but no other pre-defined function

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>

#include<stdlib.h>

struct coin_prob{

int heads;

int flips;

};

void coin_flip(struct coin_prob * coin)

{

    // generate a random number

    int n = rand();

   

    // even numbers for Tails

    if( n % 2 == 0 )

    {

        printf("Outcome : Tails\n");

       

        // increase the number of flips

        coin->flips++;

    }

    // odd numbers for Heads

    else

    {

        printf("Outcome : Heads\n");

       

        // increase the number of heads

        coin->heads++;

        

        // increase the number of flips

        coin->flips++;

    }

}

int main()

{

    struct coin_prob * coin = (struct coin_prob *)malloc( sizeof(struct coin_prob) );

   

    coin->heads = 0;

    coin->flips = 0;

   

    coin_flip(coin);

    coin_flip(coin);

    coin_flip(coin);

    coin_flip(coin);

    coin_flip(coin);

   

    printf("\nNo of heads : %d\nNo of flips : %d", coin->heads, coin->flips);

   

    return 0;

}

Add a comment
Know the answer?
Add Answer to:
Simulate the flipping of a coin Print the result of the coin flip : use off...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You have a biased coin, where the probability of flipping a heads is 70%. You flip...

    You have a biased coin, where the probability of flipping a heads is 70%. You flip once, and the coin comes up tails. What is the expected number of flips from that point (so counting that as flip #0) until the number of heads flipped in total equals the number of tails?

  • In C++ please Create a coin-flipping game. Ask the user how many times to flip the...

    In C++ please Create a coin-flipping game. Ask the user how many times to flip the coin, and use the random function to determine heads or tails each time a coin is flipped. Assume the user starts with $50. Every time the coin is flipped calculate the total (heads +$10, tails -$10). Create another function to test if the user has gone broke yet (THIS FUNCTION MUST RETURN A BOOLEAN TRUE/FALSE VALUE). End the program when the user is broke...

  • Problem 02 Design a finite-state machine that records the results of flipping a coin a certain nu...

    Problem 02 Design a finite-state machine that records the results of flipping a coin a certain number of times. The state machine takes as an input flip which is zero for heads and one for tails. The states are labeled so as to indicate the numbers of heads and the nurnber of tails, NONE. H. T, HH, TT HT, HHH, etc. The order that the heads or tails was flipped does not matter, only the total numbers of heads and...

  • "Flip a Coin!" You will write a program that allows the user to simulate a coin...

    "Flip a Coin!" You will write a program that allows the user to simulate a coin flip. Note the following line of code: (Math.random()*2) +1; This line generates a random number (a float) between 1 and 2. If you wanted to simulate somebody rolling a die, you could store the value of this variable as: var coinFlip = (Math.random()*2) +1; (Be sure to include all the parentheses in this line of code.) Reminder - Of course, you’ll need to convert...

  • casino Carl loves flipping coins. In fact, he is preparing to flip a coin 50 times...

    casino Carl loves flipping coins. In fact, he is preparing to flip a coin 50 times and track how many heads he gets (from zero to 50) Casino Carl loves flipping coins. In fact,he is preparing to flip a coin 50 times and track how many heads he gets (from zero to 50). Use the normal approximation to find the probability Carl gets 20 heads or less from his 50 flips? Select one: a. 0.1563 b. 0.1014 C.0.0986 d. 0.0793

  • Suppose you and your roommate use a coin-flipping app to decide who has to take out...

    Suppose you and your roommate use a coin-flipping app to decide who has to take out the trash: heads you take out the trash, tails your roommate does. After losing a number of flips, you start to wonder if the coin-flipping app really is totally random, or if it is biased in one direction or the other. To be fair to your roommate, you wish to test whether the app is biased in either direction, and thus a two-tailed test...

  • LabVIEW Homework (On NI LabVIEW Program) Write a program to simulate an experiment of flipping a...

    LabVIEW Homework (On NI LabVIEW Program) Write a program to simulate an experiment of flipping a coin a number of times (for example 100 times), and show the number of times getting heads and tails respectively. You can use random numbers to simulate the experiment. If the random number is >0.5, you can assume it is one side (for example Head), otherwise is the other side (for example Tail).

  • write a function simulate the rolling of a roulette wheel (38 possible outcomes numbered 0-37)print the...

    write a function simulate the rolling of a roulette wheel (38 possible outcomes numbered 0-37)print the result of the roll (0-37). update the number of times the result is equal to 17. update the number of times the wheel has been rolled. struct prob17{    int times17; int rolls; }; void rollWheel(struct prob 17 * wheel)

  • Casino Carl loves flipping coins. In fact, he is preparing to flip a coin 50 times...

    Casino Carl loves flipping coins. In fact, he is preparing to flip a coin 50 times and track how many heads he gets (from zero to 50). Use the normal approximation to find the probability Carl gets 20 heads or less from his 50 flips?

  • 1. A coin is flipped 200 times. Use the Binom.dist function to determine the probability that...

    1. A coin is flipped 200 times. Use the Binom.dist function to determine the probability that there are between 39 and 103(inclusive) heads flipped. 2. Use Rand() to generate 1000 random numbers from 0-1 and then use Binom.inv to show the number of heads in flipping a coin 200 times for each of the random numbers.Next prove the average number of heads from the 1000 is close to 100. Use the average function with the results of the Binom.inv.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT