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

Code is not properly displaying the right answer it should look more like #inclu

ID: 3598714 • Letter: C

Question

Code is not properly displaying the right answer it should look more like

#include<iostream>
#include<ctime>
#include<string>
#include<iomanip>

//Name space declaration

using namespace std;

//Starting of main function

int main()
{

//Variable data type declaration
int res;
int i;
int payoff=1;
int total;
int pay;
float Ftotal = 0;
float Average;

cout<<"Game is starting.. ";

//Starting of for loop
//Will run 10 times
for(i=0;i<10;i++)

{

//Infinite while loop, when res == 0 loop will terminate
while(1)
{

res = rand() % 2;

if(res == 1)

{

cout<<"T";
//Calculating payoff for Tail count
payoff = payoff * 2;

}

if(res==0)

{

cout<<"H";

pay = 2;

break;

}

}

//Calculating total win payoff for game played once;

total = payoff * pay;

//Calculating All win payoff
Ftotal = Ftotal + total;

payoff = 1;

cout<<" You win $"<<total<<endl;

}

//Calculating Average

Average = Ftotal/10;

//Displaying Average

cout<<"Average payoff is: $"<<fixed<<setprecision(2)<<Average<<endl;

return 0;

}

TTTH TH TH You win $16.00 You win $4 You win $4 You win $2 .00 .00 .00 TTTTHYou win $32.00 TH You win $4 You win $2 You win $2 You win $4 You win $4 .00 .00 .00 .00 .00 TH TH The average payout was $7.40

Explanation / Answer

This code is working properly and thers are no mistakes in the code.

The output of the program is totally depends on the random

number generation.

If rand() function generates sequence 1 1 1 0 in 1st iteration

If rand() function generates sequence 1 0 in 2nd iteration

If rand() function generates sequence 1 0 in 3rd iteration

If rand() function generates sequence 0 in 4th iteration

If rand() function generates sequence 1 1 1 1 0 in 5th iteration

If rand() function generates sequence 1 0 in 6th iteration

If rand() function generates sequence 0 in 7th iteration

If rand() function generates sequence 0 in 8th iteration

If rand() function generates sequence 1 0 in 9th iteration

If rand() function generates sequence 1 0 in 10th iteration

then outputs will be as mentioned in screen.

Hence the ouput is totally depends on rand() and it is different

in each execution.