Advertisement
ssoni

streak.c

Feb 28th, 2022
1,049
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.92 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <cs50.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. int main(int argc, string argv[])
  7. {
  8.     if (argc != 2)
  9.     {
  10.         printf("USAGE: ./streak LENGTH\n");
  11.         return 1;
  12.     }
  13.  
  14.     int streakTarget = atoi(argv[1]);
  15.     int currentStreak = 1;
  16.     int prevFlip = 999;
  17.     int totalFlips = 0;
  18.  
  19.     srand(time(0));
  20.     int flip=0;
  21.  
  22.     while (currentStreak < streakTarget)
  23.     {
  24.         //Flip the coin
  25.         flip = rand() % 2;
  26.  
  27.         if (flip == 0)
  28.         {
  29.             printf("H");
  30.         }
  31.         else
  32.         {
  33.             printf("T");
  34.         }
  35.  
  36.         //check streak
  37.         if (flip == prevFlip)
  38.         {
  39.             currentStreak++;
  40.         }
  41.         else
  42.         {
  43.             currentStreak = 0;
  44.         }
  45.  
  46.         prevFlip = flip;
  47.         totalFlips++;
  48.     }
  49.  
  50.     printf("\nIt took %i flips to get a steak of %i\n", totalFlips, streakTarget);
  51.  
  52. }
  53.  
  54.  
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement