Question: Very short description: Write a program in c++ that simulates a coin tossing game for 2 players. The program should toss the coin 100 times.

Very short description:

Write a program in c++ that simulates a coin tossing game for 2 players. The program should toss the coin 100 times. Ask the other player after each toss to guess the toss and then output the aggregate of Heads or Tails guess made.

More detailed description:

The program should allow the players to choose if they want to player 1 or player 2. The player 1 has the first go. Player 1 then tosses the coin and asks player 2 to make the guess if it is Heads or Tails. If the guess is right, then player2 gets awarded 10 pts. If the guess is wrong then the player loses 5 pts (-5pts). At the end of 100 tosses, calculate the confidence %, and display a formatted result as the example below: Trials #of Heads #of Tails Player1 Score Player2 Score 100 50 50 70% (3500) 30% (1500)

Write a function toss to enable player to flip the coin. toss takes no input and outputs 1 for Heads and 0 for Tails. Write a function guess to ask a player to guess if the toss was Heads or Tails by inputting H for Heads and T for Tails. guess takes in no inputs from main() and returns 1 for Heads and 0 for Tails. Write a function confidence to calculate the percentage of right guesses made by players. For example: If player 1 guesses right 35 out of 50 times then his confidence% is 70%. Note that out of 100 coin tosses, each player gets 50 times each to toss and to guess. If your program simulates a realistic coin toss, then out of the 100 tosses, you should have 50 heads and 50 tails. The player with highest score is declared the winner and gets a congratulatory message as Well done!!! Player X is the Winner!

To simulate the coin toss, the program must generate a random number from 0 to 1 for each toss for Tails and Heads respectively. To generate a random number, C provides the rand() function. However, this function is a pseudo-random number generator. The very first time the rand() function is called, it will come up with the same number (not exactly what you want). Therefore, to give the rand function a different starting point each time, we can initialize the random generator with the time() function, which returns the number of seconds from the system clock.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!