C++ :Write a program that simulates flipping a coin to make decisions. The input is how many decisions are needed, and the output is either heads or tails. Assume the input is a value greater than 0. If the input is 3, the output is:a.headsb.tailsc.headsFor reproducibility needed for auto-grading, seed the program with a value of 2. In a real program, you would seed with the current time, in which case every program run output would be different, which is what is desired but can't be auto-graded. Your program must define and call a function: string HeadsOrTails() that returns "heads" or "tails".

NOTE: A common student mistake is to call srand() before each call to rand(). But seeding should only be done once, at the start of the program, after which rand() can be called any number of times.

Respuesta :

Answer:

#include <iostream>

#include <cstdlib>

using namespace std;

int t;

char HeadsOrTails(){

char a='h';

char b='t';

if(t%2==0)

return a;

else

return b;

}

int main ()

{

 int n,  c, b;

   

 cout << "Enter the number of decisions you want" << endl;

 cin >> n;

 b=1;

 cout << "Decisions are:" << endl;

  srand(100);

 for (c = 1; c <= n; c++)

 {

   

   t = rand();

   if(HeadsOrTails()=='h'){

    cout<<b<<".heads"<<endl;

    b++;

   }

   else{

    cout<<b<<".tails"<<endl;

    b++;

   }

   

 }

 return 0;

}

Explanation:

random number generator is used to generate a number and then heads and tails is decided on even and odd number basis. if number is even then decision is head and if number is odd then decision in tails

The program is an illustration of random module.

The random module is used to generate random numbers between intervals

The program in C++, where comments are used to explain each line is as follows:

#include <iostream>

#include <cstdlib>

using namespace std;

//This declares the HeadsOrTails function

string HeadsOrTails(){

   //This checks if the random number is even

   if(rand()%2==0){

       //If yes, this returns "head"

   return "Head";

   }

   //It returns "tail", if otherwise

   return "Tail";

}

//The main begins here

int main (){

   //This declares the number of games

   int n;

   //This prompts the user for input

   cout << "Number of games: ";

   //This gets the input

   cin >> n;

   //This seeds the random number to 2

   srand(2);

   //This prints the output header

   cout << "Result: ";

   //The following iteration prints the output of the flips

   for (int c = 1; c <= n; c++){

       cout<<HeadsOrTails()<<" ";

}

return 0;

}

At the end of the program, the result of each flip is printed.

Read more about similar programs at:

https://brainly.com/question/16930523

Q&A Education