Question

Write a C program that prompts the user for the number for times the user wants...

Write a C program that prompts the user for the number for times the user wants to flip a coin. Your program will flip a virtual coin that number of times and return the following:

1) The number of times heads occurred.

2) The number of times tails occured.

Then take the coin flip program and modify the random number generator to roll a single 6-sided dice.  

DO NOT DISPLAY THE RESULTS OF EACH ROLL.

1) Ask the user how many times to roll the dice.

2) Roll the dice that many times and keep track of how many times each number is rolled.

3) Display a nicely formatted tally of how many times each number was rolled.

Please type and display output.

Thank you

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

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
    int seedVal = 0, i;
    int choice, user, userCount = 0, computerCount = 0;
    char again;
    srand(time((time_t*)&seedVal));
    while (1) {
        printf("0 - Heads wins\n1 - Tails win\n-1 - quit\nEnter choice: ");
        scanf("%d", &choice);
        if (choice == 1 || choice == 0) {
            user = rand() % 2;
            if (user == choice) {
                userCount++;
                printf("User wins\n");
            }
            else {
                computerCount++;
                printf("Computer wins\n");
            }
            printf("Do you want play again (y/n): ");
            scanf("%c", &again);
            scanf("%c", &again);
            if (again == 'n') {
                break;
            }
        }
        else {
            break;
        }
    }
    printf("User win count = %d\n", userCount);
    printf("Computer win count = %d\n", computerCount);
    return 0;
}

- Heads wins 1quit nter choice: 1 serWlNs Do you want play again (y/n ): y -Heads Wins 1quit nter choice: 1 serWlNs Do you wa

\color{red}Please\; upvote\;the \;solution \;if \;it \;helped.\;Thanks!

Add a comment
Know the answer?
Add Answer to:
Write a C program that prompts the user for the number for times the user wants...
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
  • 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...

  • question 2 in C programming please PE-05b-01 (Six-sider) write a counter-controlled program that prompts the user...

    question 2 in C programming please PE-05b-01 (Six-sider) write a counter-controlled program that prompts the user to enter the number of times n to roll a six-sided die. The program should print to the screen the iteration of the loop and result for each roll 1 ) How many times would you like to roll? 3 roll 6 6 5 1 2) PE 05b 02 (Flash Cards) Write a counter-controlled program that creates addition problems for an elementary kid to...

  • in java Write an application that: 1. Prompts the user for a number of trials to...

    in java Write an application that: 1. Prompts the user for a number of trials to perform (I will call this "N"). This operation should be type-safe for integers. 2. Simulates N trials of rolling two dice and finding the sum. 3. Uses an array to keep track of how many times each outcome has been rolled (you do not need to keep track of the actual rolls themselves). 4. Computes the percentage of the total number of rolls for...

  • Practice: • function • loops • if condition • random Roll a dice many times, you...

    Practice: • function • loops • if condition • random Roll a dice many times, you will see that each face value will count of roughly 1/6 • write a function • roll a dice once, return value will be 1-6, • use your functin I call your function 6000 times • count how many times of face 1 • count how many times of face 2 • print out the numbers of total face 1, 2, ..., 6 you...

  • Please help me put this in order!! THANKYOU!! (CO 4 and 6) Create a program with...

    Please help me put this in order!! THANKYOU!! (CO 4 and 6) Create a program with a function named flipCoin that will flip a coin as many times as the user asks. The flipCoin function should return “heads” or “tails”. The program should continue while the user presses Y. SAMPLE OUTPUT: How many times do you want to flip a coin? 5 You got: tails You got: tails You got: tails You got: tails You got: heads To continue press...

  • write a c++ program that prompts a user for a number then attempts to allocate an...

    write a c++ program that prompts a user for a number then attempts to allocate an array of as many integers as the user enters. In other words, the program might prompt the user with a line like: “How many integers would you like to allocate?” It should then allocate as many integers as the user enters. The program should then wait for the user to press enter before deleting the allocated array and quitting. We will use this time...

  • 3) We roll 2 fair dice. a) Find the probabilities of getting each possible sum (i.e....

    3) We roll 2 fair dice. a) Find the probabilities of getting each possible sum (i.e. find Pr(2), Pr(3), . Pr(12) ) b) Find the probability of getting a sum of 3 or 4 (i.e.find Pr(3 or 4)) c) Find the probability we roll doubles (both dice show the same value). d) Find the probability that we roll a sum of 8 or doubles (both dice show the same value). e) Is it more likely that we get a sum...

  • Flip a coin 10 times and record the observed number of heads and tails. For example,...

    Flip a coin 10 times and record the observed number of heads and tails. For example, with 10 flips one might get 6 heads and 4 tails. Now, flip the coin another 20 times (so 30 times in total) and again, record the observed number of heads and tails. Finally, flip the coin another 70 times (so 100 times in total) and record your results again. We would expect that the distribution of heads and tails to be 50/50. How...

  • Write a C++ program that simulates tossing a coin. Allow the user to enter the number...

    Write a C++ program that simulates tossing a coin. Allow the user to enter the number of tosses. Print the number of tosses that yielded heads and the number of tosses that yielded tails. use function

  • Problem: Write a function named coinToss that simulates the tossing of a coin. When you call the function, it should ge...

    Problem: 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 the tossing of the coin that...

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