Question

int coinToss() 10 const int MAX VALUE = 1; const int MIN_VALUE-3; 12 13 // Get the system time unsigned seed time(9); 15 16 1

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 number of times.

Can you please fix my code so the program works properly.

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

If you have any doubts, please give me comment...

#include<iostream>

#include<cstdlib>

using namespace std;

int coinToss();

int main(){

srand(time(0));

int numberFlips, heads = 0, tails = 0, outcome;

cout<<"This program will toss a coin and display the results"<<endl;

cout<<"Please enter the amount if times you wish to flip the coin "<<endl;

cin>>numberFlips;

for(int count=1; count<=numberFlips; count++){

outcome = coinToss();

if(outcome==1)

heads++;

else if(outcome==2)

tails++;

}

cout<<"The total number of heads: "<<heads<<endl;

cout<<"The total number of tails: "<<tails<<endl;

return 0;

}

int coinToss(){

const int MAX_VALUE = 3;

const int MIN_VALUE = 1;

int randomNumber = (rand()%(MAX_VALUE-MIN_VALUE))+MIN_VALUE;

if(randomNumber==1)

cout<<"heads"<<endl;

else

cout<<"tails"<<endl;

return randomNumber;

}


Add a comment
Know the answer?
Add Answer to:
Problem: Write a function named coinToss that simulates the tossing of a coin. When you call the function, it should ge...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • 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...

  • 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...

  • 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

  • 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...

  • 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...

  • a. Write a function named findmax() that finds and displays the maximum values in a two...

    a. Write a function named findmax() that finds and displays the maximum values in a two dimensional array of integers. The array should be declared as a 10 row by 20 column array of integers in main() and populated with random numbers between 0 and 100. b. Modify the function written above so that it also displays the row and column numbers of the element with the maximum value. I have this so far, and but it's only finding the...

  • # 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...

  • 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...

  • Find and fix errors #include <iostream> #include <cstdlib> #include <ctime> using namespace std; const int MIN...

    Find and fix errors #include <iostream> #include <cstdlib> #include <ctime> using namespace std; const int MIN = 1; const int MAX = 10; int getRandom(int low, int high); int main() {    int random_num = 0; int player_num; int tries; int seed = static_cast<int>(time(0)); bool guessed = false;    srand(seed); // call the getRandom function below       tries = 4; while ( tries > 0 && !guessed ) { cout << "Enter a number within the range 1 to...

  • Hello, I am working on a C++ pick 5 lottery game that gives you the option...

    Hello, I am working on a C++ pick 5 lottery game that gives you the option to play over and over. I have everything working right except that every time the game runs it generates the same winning numbers. I know this is an srand or rand problem, (at least I think it is), but I can't figure out what my mistake is. I've tried moving srand out of the loop, but I'm still getting the same random numbers every...

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