In: Computer Science
(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
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;
}