Question

In: Computer Science

(C++)Problem #1: Coin Tossing Simulation: Write a program that simulates coin tossing. Let the program prompt...

(C++)Problem #1: Coin Tossing Simulation: Write a program that simulates coin tossing. Let the program prompt the user to enter the number of tosses N and count the number of times each side of the coin appears. Print the results. The program should call a separate function flip() that takes no arguments and returns 0 for tails and 1 for heads. The program should produce different results for each run.

Sample Input / Output

Enter the number of tosses N: 1000

The total number of Heads is: 495

The total number of Tails is: 505

Solutions

Expert Solution

Source Code:

Output:

Code in text format (See above images of code for indentation):

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
/*main function*/

int main()
{
   /*function prototype*/
   int flip();
   /*variables*/
   int N,heads=0,tails=0,i,toss;
   /*read number of tosses from user*/
   cout<<"Enter the number of tosses N: ";
   cin>>N;
   srand(time(0));
   /*iterate loop upto N*/
   for(i=0;i<N;i++)
   {
       /*function call to toss a coin*/
       toss=flip();
       /*if toss is head*/
       if(toss==1)
           heads++;
       /*if toss is tail*/
       else if(toss==0)
           tails++;
   }
   /*print number of heads and tails*/
   cout<<"The total number of Heads is: "<<heads<<endl;
   cout<<"The total number of Tails is: "<<tails<<endl;
   return 0;
}
/*function definition*/
int flip()
{
   /*toss a coin*/
   int toss=rand()%2;
   /*return toss*/
   return toss;
}


Related Solutions

In C++  Write a program that simulates coin tossing. For each toss of the coin the program...
In C++  Write a program that simulates coin tossing. For each toss of the coin the program should print heads or tails. Let the program toss the coin 100 times and count the number times each side of the coin appears. Print the results. 0 represents tails and 1 for heads.
Write an application that simulates coin tossing. Let the program toss a coin each time the...
Write an application that simulates coin tossing. Let the program toss a coin each time the user chooses the “Toss Coin” menu option. Count the number times each side of the coin appears. Display the results. The program should call a method flip( ) that takes no arguments and returns a zero to represent a tail or a one to represent a head. There is a Random class that will allow you to generate a random integer. Import it from...
Problem 1 [20 pts]: An experiment consists of tossing a coin 6 times. Let X be...
Problem 1 [20 pts]: An experiment consists of tossing a coin 6 times. Let X be the random variable that is the number of heads in the outcome. Find the mean and variance of X. Thank You
Write a program that determines the probability of tossing a coin 10 times and getting exactly...
Write a program that determines the probability of tossing a coin 10 times and getting exactly 0, 1, 2, 3, etc. heads. This is the binomial probability distribution. Store the probability in an array. You could get 0 heads or 10 heads or anything inbetween. Use a for loop. The for loop will go from 0 to 10 inclusive. Use r as the number of successes. So r will go from 0 to 10. The probability of a success is...
Write a program that simulates flipping a coin repeatedly until three consecutive        heads are...
Write a program that simulates flipping a coin repeatedly until three consecutive        heads are tossed. The program should then display the total number of times the coin was        flipped. The user does not have to enter any information and we must use a while loop.
Write a program that simulates the flipping of a coin n times, where n is specified...
Write a program that simulates the flipping of a coin n times, where n is specified by the user. The program should use random generation to flip the coin and each result is recorded. Your program should prompt the user for the size of the experiment n, flip the coin n times, display the sequence of Heads and Tails as a string of H (for Head) and T (for Tail) characters, and display the frequencies of heads and tails in...
C++ Create a program that simulates a coin being flipped. Ask the user to guess between...
C++ Create a program that simulates a coin being flipped. Ask the user to guess between heads and tails. Let the user input 0 for heads and 1 for tails. Use a random generator to get random guesses every time. If the user guesses correctly, give them 1pt. Use a counter and initialize it to 0.   If the user does not guess correctly, subtract a point. Create a menu that allows the user to continue guessing, view the current score...
1. Write a program in C++ to find the factorial of a number. Prompt the user...
1. Write a program in C++ to find the factorial of a number. Prompt the user for a number and compute the factorial by using the following expression. Use for loops to write your solution code. Factorial of n = n! = 1×2×3×...×n; where n is the user input. Sample Output: Find the factorial of a number: ------------------------------------ Input a number to find the factorial: 5 The factorial of the given number is: 120 2. Code problem 1 using While...
IN C This assignment is to write a program that will prompt the user to enter...
IN C This assignment is to write a program that will prompt the user to enter a character, e.g., a percent sign (%), and then the number of percent signs (%) they want on a line. Your program should first read a character from the keyboard, excluding whitespaces; and then print a message indicating that the number must be in the range 1 to 79 (including both ends) if the user enters a number outside of that range. Your program...
Write a C program Your program will prompt the user to enter a value for the...
Write a C program Your program will prompt the user to enter a value for the amount of expenses on the credit card. Retrieve the user input using fgets()/sscanf() and save the input value in a variable. The value should be read as type double. The user may or may not enter cents as part of the input. In other words, expect the user to enter values such as 500, 500.00 and 500.10. The program will then prompt the user...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT