Write a C++ program that prompts the user to input the number of times he/she wa
ID: 3597340 • Letter: W
Question
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 fiunctions to generate random numbers. Sample input / output: low many times do you want to toss the coin? 100e ipping a coin one nillon tines.. eads: 501? ails: 4983 Hou many tines do you want to toss the coin? 10000 ipping a coin one nillon tines.. Heads 4996 Tails: 5004Explanation / Answer
Below is your code:-
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
int currentTotalTails = 0;
int currentTotalHeads = 0;
int LOOP;
cout<<"How many times do you want to toss the coin? ";
cin>>LOOP;
cout<<"Flipping a coin "<<LOOP<<" times....."<<endl;
srand(time(NULL));
for (int i = 0; i < LOOP; ++i) //tosses a coin LOOP times
{
if (rand() % 2 == 0)// heads
{
currentTotalHeads++;
}
else // tails
{
currentTotalTails++;
}
}
cout << "Heads: " << currentTotalHeads << endl;
cout << "Tails: " << currentTotalTails << endl;
system("pause");
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.