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

Lab 6.2 Functions which return values In C++, Write a function named cointoss th

ID: 663107 • Letter: L

Question

Lab 6.2 Functions which return values


In C++, Write a function named cointoss that simulates the tossing of a coin.

When you call the function, it should generate a random number in the range of 1 through 2.

If the random number is 1, the function should display "heads".

If the random number is 2, the function should display "tails".

Demonstrate the function in a program that asks the user how many times the coin should be tossed, and then simulates tossing the coin that number of times.

Report the total number of heads and tails.

Explanation / Answer

int h=0;

int t=0;

if(randomInt == 1) {

System.out.println("Heads");

h++;

}

else {

System.out.println("Tails");

t++;

}

}

System.out.println("Total number of heads are : "+ h);

System.out.println("Total number of tails are : "+ t);