Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <cs50.h>
- #include <stdlib.h>
- #include <time.h>
- int main(int argc, string argv[])
- {
- if (argc != 2)
- {
- printf("USAGE: ./streak LENGTH\n");
- return 1;
- }
- int streakTarget = atoi(argv[1]);
- int currentStreak = 1;
- int prevFlip = 999;
- int totalFlips = 0;
- srand(time(0));
- int flip=0;
- while (currentStreak < streakTarget)
- {
- //Flip the coin
- flip = rand() % 2;
- if (flip == 0)
- {
- printf("H");
- }
- else
- {
- printf("T");
- }
- //check streak
- if (flip == prevFlip)
- {
- currentStreak++;
- }
- else
- {
- currentStreak = 0;
- }
- prevFlip = flip;
- totalFlips++;
- }
- printf("\nIt took %i flips to get a steak of %i\n", totalFlips, streakTarget);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement