Problem 1: Random Heads and Tails Simulation Write a C++ program that prompts th
ID: 3606959 • Letter: P
Question
Problem 1: Random Heads and Tails Simulation Write a C++ program that prompts the user to input the number of times he/she wants to simulate flipping of a coin" game. After the completion of all the rounds, the program should print the number of heads and tails occurred during the random trials. Since, each trial of the experiment will be random; the final results should be different at each run. Hint: You can use rand0 and srand0 built-in functions to generate random numbers. Sample input/ output: ow nany tines do you want to toss the coin? 10006 ipping a coin one nillon tines.. eads: 5817 ails: 4983 ow nany tines do you want to toss the coint 10000 ipping a coin one nillon tines.. eads: 4996 ails: 5804Explanation / Answer
#include <iostream>
#include<stdlib.h>
#include <time.h>
using namespace std;
int main()
{
srand(time(NULL));
int heads=0;
int tails=0;
int n;
cout<< "How many times do you want to toss a coin";
cin>>n;
cout<<"Flipping a coin "<< n << " times"<<endl;
for(int i=0;i<n;i++)
if(rand()%2==0)
tails++;
else
heads++;
cout<< "Heads: "<<heads<<endl;
cout<< "Tails: "<<tails<<endl;
}
The outputs are :
How many times do you want to toss a coin2000
Flipping a coin 2000 times
Heads: 1024
Tails: 976
How many times do you want to toss a coin2000
Flipping a coin 2000 times
Heads: 982
Tails: 1018
How many times do you want to toss a coin2000
Flipping a coin 2000 times
Heads: 1027
Tails: 973
Please do give a thumbs up as it matters a lot.Thank You