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

1.Write a function that simulates tossing a coin. The function will need to gene

ID: 3810957 • Letter: 1

Question

1.Write a function that simulates tossing       a coin. The function will need to generate a random number and then decide whether to display "HEADS" or "TAILS". The function will return information to main to indicate heads or tails. You decide how your function should do this.

2.Write a section of code that would appear in a main function to do the following :
-Ask user how many times he wants you to toss a coin.
-Write a code to "toss the coin" the requested number of times. You must call the function you wrote from question 1.
-At the conclusion, print out the number of times HEADS appeared, and the number of times TAILS appeared.

In c++ language pls. You are only allowed to use the following functions on number 1.:
#include<iostream>
#include<iomanip>
#include<ctime>
#include<cstdlib>

Explanation / Answer

#include<iostream>
#include<iomanip>
#include<ctime>
#include<cstdlib>

using namespace std;

int toss() //this simulates tossing a coin
{
   int x = rand()%2;
   return x;                       //returns 0 for HEADS and 1 for TAILS
}
int main()
{
   srand(time(NULL));

   int num;
   cout<<"How many times do you want to toss the coin: ";
   cin>>num;

   int h = 0, t = 0;

   for(int i=0;i<num;i++)
   {
       if(toss() == 0)
           h++;
       else
           t++;
   }
   cout<<"HEADS: "<<h<<" TAILS: "<<t<<endl;

}

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