Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

create a C++ program that simulates the tossing of a coin. Write a function name

ID: 3534655 • Letter: C

Question

create a C++ program that simulates the tossing of a coin. Write a

function named coinToss. 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 simulate the tossing of the coin that number of times.

Write a second function that summarizes the number of “heads†and the number of “tailsâ€.

Explanation / Answer

#include<iostream>

#include<ctime>

using namespace std ;


int coinToss(){

int i=0;

srand(time(NULL));

i=rand() % 2 + 1;

if(i==1)

cout<<"Heads";

else

cout<<"tails";

return i ;

}

void count(int i,int t){

cout<<"Number of Heads :"<<i;

cout<<"Number of tails :"<<t;

}

int main(){

int i =0,x=0,num,countHead=0,countTail=0;

cout<<"Number of toss";

cin>>num;

for(i=0;i<num;i++){

x=coinToss();

if(x==1)

countHead++;

if(x==2)

countTail++;

}

count(countHead,countTail);

return 0 ;

}