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

Write a section of code that could appear in a main function to do the following

ID: 3814627 • Letter: W

Question

Write a section of code that could appear in a main function to do the following.
A:Ask The user how many times he wants to toss a coin.
B :Write code to "Toss the coin" the requested number of times. You must call the function you wrote above.
C:At the conclusion, print out the number or times HEADS appeared, and number of times TAILS appeared.

Below is the code. I just want to know the last part of the question. That is C

Cout << "How many times you want to Toss the coin? " << endl;
cin>>Times;
for (num = 0; num < Times; num++) {
if (Toss==1)
Heads++;
else
Tails++;
}

Explanation / Answer

I was not able to run your code as it was partial, So created my own code for this. It has solution for all three parts of your question. please see below the code

#include<iostream>
using namespace std;
#include<conio.h>
#include<ctime>
#include <cstdlib>

//protoype of method to calculate heads and tails
void calHeadsNTails(int, int&, int&);
//main mathod
int main()
{
   srand(static_cast<int>(time(0)));// seeding the random function with time
   int Tails, Heads, Times;
cout << "How many times you want to Toss the coin? ";
   cin>>Times;
calHeadsNTails(Times, Heads, Tails);// pass by reference of numHeads and numTails
cout << "Total number of heads: " << Heads<< endl;
cout << "Total number of tails: " << Tails<< endl;
return 0;
}

void calHeadsNTails(int totalFlips, int &numHeads, int &numTails)
{
int flip = 0;
numHeads =0;
numTails=0;
for (int i = 0; i < totalFlips; i++)
{
flip = rand() % 2;// generating random from 0 and 1. 0 = heads and 1 =tails
if(flip == 0)
numHeads++;
else
numTails++;
}

}

Output: -

How many times you want to Toss the coin? 30
Total number of heads: 18
Total number of tails: 12

--------------------------------
Process exited after 0.9568 seconds with return value 0
Press any key to continue . . .

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