Advertisement
garryhtreez

6.33

Dec 14th, 2020
630
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.66 KB | None | 0 0
  1. /*6.33 Coin Tossing
  2.    Deitel & Deitel C++ How to Program, 10th ed (Indian subcontinent adaptation)
  3.     Visual Studio Community 2019
  4. */
  5.  
  6. #include <iostream>
  7. #include <cstdlib>
  8. #include <ctime>
  9. using namespace std;
  10.  
  11. int coinToss(void) {
  12.     return (1 + rand()) % 2;
  13. }
  14.  
  15. int main() {
  16.     srand(static_cast<unsigned int>(time(0)));
  17.     int heads{ 0 };
  18.     int tails{ 0 };
  19.     for (int i = 0; i < 100; i++) {
  20.         if (coinToss() == 1) {
  21.             cout << "Heads" << endl;
  22.             heads++;
  23.         }
  24.         else {
  25.             cout << "Tails" << endl;
  26.             tails++;
  27.         }
  28.     }
  29.     cout << endl << "Final tally:" << endl << endl;
  30.     cout << "\t Heads  " << heads;
  31.     cout << "\t Tails  " << tails << endl;
  32.     return 0;
  33. }
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement