Question

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 face of the coin
  • void count() - called from toss to increment counter for heads or tails
  • void displayCount() - is called from main() and will display the values of the counter for heads and the counter for tails.

If any global variables are used in the program, the program will receive a grade of 0.

So far what I have. Anything I should clean up? Also, I am confused on the global variables part specifically. Can you doublecheck to make sure I am not using any in the program. Any help is appreciated!!

#include
#include
#include
using namespace std;
int totalCount=100;
int headsCount=0;
int currentCoinFace;
void count()
{
   if(currentCoinFace==0)
   {
       headsCount++;
       cout<<"Heads"<    }
   else
   {
       cout<<"Tails"<    }
}
void toss()
{
   currentCoinFace=rand() % 2;  
   count();
}
void displayCount()
{
   cout<<"The total number of heads ="<    cout<<"The total number of heads ="< }
int main()
{
   srand (time(NULL));
   for(int i=0;i    toss();
   displayCount();
}

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

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

using namespace std;

void count(int &headsCount, int currentCoinFace) {
   if(currentCoinFace==0) {
       headsCount++;
       cout<<"Heads"<<endl;  
   }
   else {
       cout<<"Tails"<<endl;
   }
}

void toss(int ¤tCoinFace) {
   currentCoinFace=rand() % 2; 
}

void displayCount(int headsCount, int totalCount) {
   cout<<"The total number of heads ="<<headsCount<<endl;
   cout<<"The total number of heads ="<<totalCount-headsCount<<endl;
}

int main() {
        int totalCount=100;
        int headsCount=0;
        int currentCoinFace;
        srand (time(NULL));
        
        for(int i=0;i<totalCount;i++) {
                toss(currentCoinFace); 
                count(headsCount, currentCoinFace);
        }
        
        displayCount(headsCount, totalCount);
}


Please upvote, as i have given the exact answer as asked in question. Still in case of any issues in code, let me know in comments. Thanks!

Add a comment
Know the answer?
Add Answer to:
Write a C++ program that simulates coin tossing. For each toss of the coin the program...
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...

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

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

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

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

  • 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 program called CountFlips whose main method flips a coin 100 times and counts how...

    Write a program called CountFlips whose main method flips a coin 100 times and counts how many times each side comes up. Make sure to include comments of what is being done in the code. Verify whether the head comes up or not by calling isHeads method of Coin class. Increment the heads count if head comes up. Increment the tails count if tail comes up. Display the head and tails counts. Print the results SEND AS A TEXT FILE...

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

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