C++ LANGUAGE Write a function named coinToss that simulates the tossing of a coi
ID: 3686055 • Letter: C
Question
C++ LANGUAGE
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 number of times.
Don't forget you need:
do/while loop(s) for input validation
use pass by value and pass by reference as needed
Explanation / Answer
#include “stdafx.h”
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
void coinToss();
int main()
{
int times;
cout<<“How many times the coin should be tossed?”;
cin>>times;
for(int i=1;i<=times;i++)
{
coinToss();
}
system(“pause”);
return 0;
}
void coinToss()
{
int randomNumber;
unsigned seed=time(0);
srand(seed);
randomNumber=rand()%2+1;
if(randomNumber==1)cout<<“heads”;
else if (randomNumber==2)cout<<“tails”<<endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.