Question

Write a program that simulates the toss of a coin. Whenever a coin is tossed the...

Write a program that simulates the toss of a coin. Whenever a coin is tossed the result will be either a head or tail. Prompt the user as shown by entering an ‘H’ for heads or ‘T’ for tails. Use a loop for input validation of an ‘h’ or ‘t’. Make sure that both an upper case and lower case will be accepted. Have the computer generate a random number. Assign a char variable a (‘h’ ^ ’t’) head or tail so that the probability of either is equally possible. Display the outcome of the coin toss as text, either “You Win!” or “Computer Wins” as a result of the user’s guess compared to the random (‘h’ ^ ‘t’) generated. Use a loop for multiple flips of the coin. The user enters either ‘Y’ or ‘y’ to continue the program.   Keep track of the number of wins and losses. Display the summary when the user decides to quits the program. Example of the output is shown below:

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

Program:

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

//main function
int main()
{
int n, m = 0, p = 0;
char ch, yn;
do
{
//generate random number 0 or 1
int n = rand()%2;
//prompt user to enter head or tail
printf("Enter \'H\' for for heads or \'T\' for tails: ");
scanf(" %c", &ch);
//check who is won
if(n==0 && (ch=='h'||ch=='H') || n==1 && (ch=='t'||ch=='T')){
printf("You Win!\n");
p++;
}
else
printf("Computer Wins!\n");

m++;
//prompt user for continuation
printf("Are you want to continue? (y/n): ");
scanf(" %c", &yn);
}while(yn=='y' || yn=='Y');

//display summary
printf("Summary: ");
printf("Total number of tosses : %d\n", m);
printf("You win %d number of times\n", p);
printf("Computer wins %d number of times\n", m-p);
}

Output:

Add a comment
Know the answer?
Add Answer to:
Write a program that simulates the toss of a coin. Whenever a coin is tossed the...
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
  • # JAVA Problem Toss Simulator Create a coin toss simulation program. The simulation program should toss...

    # JAVA Problem Toss Simulator Create a coin toss simulation program. The simulation program should toss coin randomly and track the count of heads or tails. You need to write a program that can perform following operations: a. Toss a coin randomly. b. Track the count of heads or tails. c. Display the results. Design and Test Let's decide what classes, methods and variables will be required in this task and their significance: Write a class called Coin. The Coin...

  • LabView Coin Toss.vi Build a VI that simulates the toss of a coin. On your block...

    LabView Coin Toss.vi Build a VI that simulates the toss of a coin. On your block dia- gram, use Random Number (0-1) to generate a random floating-point number x, in the range from 0 up to (but not including1. When run, ifx 20.5, assign the result of the coin toss to be Heads; otherwise, the result is Tails. Then, use a Select icon as shown below to decide which of two strings should be sent to a front-panel String Indicator...

  • Write a C++ program that simulates coin tossing. For each toss of the coin the program...

    Write a C++ program that simulates coin tossing. For each toss of the coin the program should print Heads or Tails. The program should toss a coin 100 times. Count the number of times each side of the coin appears and print the results at the end of the 100 tosses.   The program should have the following functions as a minimum: void toss() - called from main() and will randomly toss the coin and set a variable equal to the...

  • 12. Coin Toss Simulator Write a class named Coin. The Coin class should have the following field ...

    12. Coin Toss Simulator Write a class named Coin. The Coin class should have the following field .A String named sideUp. The sideUp field will hold either "heads" or "tails" indicating the side of the coin that is facing up The Coin class should have the following methods .A no-arg constructor that randomly determines the side of the coin that is facing up (heads" or "tails") and initializes the aideUp field accordingly .A void method named toss that simulates the...

  • Using c++ Write a function named coinToss that simulates tossing a coin. When you call the...

    Using c++ Write a function named coinToss that simulates tossing a coin. When you call the function it should generate and return a random number in the range 1 through 2. 1 represents heads and 2 represents tails. Exercise 1 (40 points) Write a function named coinToss that simulates tossing a coin. When you call the function it should generate and return a random number in the range 1 through 2.1 represents heads and 2 represents tails Use the function...

  • c++ Program 2: Coin Toss Simulator For this program, please implement Problems 12 and 13 in...

    c++ Program 2: Coin Toss Simulator For this program, please implement Problems 12 and 13 in a single program (Gaddis, p812, 9E). Scans of these problems are included below. Your program should have two sections that correspond to Problems 12 and 13: 1) As described in Problem 12, implement a Coin class with the specified characteristics. Run a simulation with 20 coin tosses as described and report the information requested in the problem. 2) For the second part of your...

  • Heads or Tails Create a JavaFX application that simulates a coin being tossed. When the user clic...

    Heads or Tails Create a JavaFX application that simulates a coin being tossed. When the user clicks a button, the application should generate a random number in the range of 0 to 1. If the number is 0, the coin has landed on “heads,” and if the number is 1, the coin has landed on “tails.” Use an ImageView component, and the coin images that you will find in this book’s Student Sample Programs to display the side of the...

  • Random Number Generation and Static Methods Write a program that simulates the flipping of a coin...

    Random Number Generation and Static Methods Write a program that simulates the flipping of a coin n-times to see how often it comes up heads or tails. Ask the user to type in the number of flips (n) and print out the number of times the coin lands on head and tail. Use the method ‘random()’ from the Math class to generate the random numbers, and assume that a number less than 0.5 represents a tail, and a number bigger...

  • A coin will be tossed twice, and each toss will be recorded asheads (H...

    A coin will be tossed twice, and each toss will be recorded as heads (H) or tails (T).Give the sample space describing all possible outcomes.Then give all of the outcomes for the event that the second toss is tails.Use the formatHTto mean that the first toss is heads and the second is tails.If there is more than one element in the set, separate them with commas.

  • Problem(13) (10 points) An unfair coin is tossed, and it is assumed that the chance of...

    Problem(13) (10 points) An unfair coin is tossed, and it is assumed that the chance of getting a head, H. is (Thus the chance of setting tail, T. is.) Consider a random experiment of throwing the coin 5 times. Let S denote the sample space (a) (2 point) Describe the elements in S. (b) (2 point) Let X be the random variable that corresponds to the number of the heads coming up in the four times of tons. What are...

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