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

Problem 1 Write a program to simulate coin tosses. Since we have not yet learned

ID: 3796958 • Letter: P

Question

Problem 1 Write a program to simulate coin tosses. Since we have not yet learned about implementing randomization in a C++ code, you'll have to ask the user for the result of every toss. Your program must keep on tossing the coin until you get five straight heads. As soon as you get five straight heads, your program should display how many tosses were required to get five straight heads. (10 points) HINT a. Declare an enum data-type for head and tail b. Ask the user to enter an integer to represent the result of the toss (For example, 0 orl, 0 meaning head and 1 meaning tail) c. You will need a loop to keep on tossing the coin (i.e. asking the user for the result of the toss) d. You will need a counter to keep track of how many heads you get, and increment the counter whenever you get a head. When you get five straight heads, you can exit the loop e. You will need to reset the counter, whenever you get a tail

Explanation / Answer

Solution ::

// Header files
#include <iostream>
#include <ctime>

int Toss();
int main()
{

cout<<"Coin Simulation"<<endl<<endl;
int head=0; // define head value to 0
int tail=1; // define tail value to 1
int head_count=0; // initially headcount is zero
int tail_count=0; // initially tailcount is zero

for(int i=0; i<100; i++) // this loop will be run 100 times
{
int k=toss(); // assign value of the toss to variable k
if(k==0) //if k is equal to zero ,it seems that if k is equal to head
head_count++; //count of head will be increased
else
tail_count++; // count of tail wii be increased
}

cout<<"no.of tosses :: 100"<<endl;
cout<<"no.of heads: "<<head_count<<endl;
cout<<"no.of tails: "<<tail_count<<endl;
float p_heads=float(head_count)/100;
cout<<"P(heads)= "<<p_heads<<endl;
float p_tails=float(tail_count)/100;
cout<<"P(tails)= "<<p_tails<<endl<<endl;
cout<<"End of Coil Simulation"<<endl<<endl;

system(“pause the screen”);
}

int Toss()
{
int res_=rand()%2;
return res_;
}

/// *** Thank You *** ///

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote