Question
Create a Coin Toss Simulation: you write a function that simulates tossing a coin n times. Use integer n as an argument of your function.Call
Create a Coin Toss Simulation: you write a function that simulates tossing a coin n times.
Use integer n as an argument of your function.Call this function from main( ). In main( )
you ask user to input an integer number, then call the function by using this integer as your
functions parameter. In your function, you use a fixed-count loop that generates n random numbers. For each number generated, you identify the result as a head or tail and accumulate the results in a heads and tails counter.
flip = double (rand())/RAND_MAX; //generate a random number between 0 and 1
//flip is your tossing
if (flip > 0.5)
heads = heads + 1;
else
tails = tails + 1;
Your function prototype:voidtossing( int n);
In your function body:
Initialize a heads count to 0
Initialize a tails count to 0
srand(time(NULL))
For n times
Generate a random number between 0 and 1
If the random number is greater than 0.5
consider it a head and add 1 to the heads count
Else
consider it a tail and add 1 to the tails count
Endif
Endfor
Print the heads and tails calculated
#include
#include
#include
using namespace std;
void tossing(int n); // A program to simulate tossing a coin n times
int main(){
//input integer n
//call function tossing(n)
return 0;
}
//definition of void tossing(int n)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started