(Coin Tossing) Write a program that simulates coin tossing. For each toss of the coin, the program should print Heads or Tails. Let the program toss the coin 100 times and count the number of times each side of the coin appears. Print the results. The program should call a separate function flip that takes no arguments and returns 0 for tails and 1 for heads. [Note: If the program realistically simulates the coin tossing, then each side of the coin should appear approximately half the time.]
What will be an ideal response?
// Simulate coin tossing.
#include
#include
#include
using namespace std;
int flip(); // function prototype
int main()
{
int headsCount = 0; // total Heads count
int tailsCount = 0; // total Tails count
srand( time( 0 ) ); // seed random number generator
// simulate coin toss 100 times
for ( int loop = 1; loop <= 100; loop++ )
{
if ( flip() == 0 ) // simulate coin toss, 0 refers to tails
{
++tailsCount; // update Tails count
cout << "Tails "; // display result
} // end if
else // 1 refers to heads
{
++headsCount; // update Heads count
cout << "Heads "; // display result
} // end else
if ( loop % 10 == 0 ) // 10 tosses per line
cout << endl;
} // end for
// display totals
cout << "\nThe total number of Heads was " << headsCount
<< "\nThe total number of Tails was " << tailsCount << endl;
} // end main
// flip uses random number to simulate coin toss
int flip()
{
return rand() % 2; // scale by 2 for binary result
} // end function flip
You might also like to view...
In order to process the data in a file, first you must __________ the file and then __________ the data into memory.
a. Open, loop b. Open, Read c. Declare, Read d. Input, Write
To step through a one-dimensional array, accessing the elements one by one, it would be most appropriate to use ________ loop.
A) an infinite B) a sentinel controlled loop C) a for loop D) a nested loop E) no
Designed version of font used to set off quotes, emphasized text, foreign words, scientific names, and movie titles with appropriate differences that is not slanted normal text.
a. Italic b. Oblique c. Bold d. Faux italic
Using c*t in the Criteria row would match coat, coated, and coating
Indicate whether the statement is true or false