Print Friendly and PDF
(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.]
(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.]

Solution:
 
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
 
#include <cstdlib>
 
#include <ctime>
 
int flip(void);
 
int main()
{
   srand( time(0) );
   int t = 0;
   int h = 0;
 
   for( int i = 1; i <= 100; i++)
   {
      if(flip() == 0)
      {
         cout << "Tails" << endl;
         t++;
      }
      else
      {
         cout << "Heads" << endl;
         h++;
      }
 
   }
   cout << "Tails count: " << t << "\nHeads count: " << h << endl;
   return 0;
}
 
int flip(void)
{
   return rand() % 2;
}
zubairsaif

Zubair saif

A passionate writer who loves to write on new technology and programming

Post A Comment:

3 comments:

  1. baki programm to send kr do

    ReplyDelete
  2. please send me the questions 6.37,3.38,3.40
    emergency plzzzzzzz

    ReplyDelete
    Replies
    1. send me the complete question than i will send you the answer

      Delete