// Coin Flipping // coin_flipping.c // // This program was written by Tom Kunc (z5205060) // on 2022-03-02 // // Count the number of heads flipped, and print out whenever // a two heads are printed out in a row. #include #define NUM_FLIPS 10 #define HEADS 'h' #define TAILS 't' #define TRUE 1 #define FALSE 0 int main(void) { int coin_count = 0; int heads_count = 0; int was_last_coin_heads = FALSE; char coin; while (coin_count < NUM_FLIPS) { // TODO: Let's count heads printf("What coin side came up? "); scanf(" %c", &coin); if (coin != HEADS && coin != TAILS) { printf("Wait! That's not heads or tails!\n"); // by default, if we don't know what the // flip is, let's make it a tails coin = TAILS; } // if the last coin was heads, and this // coin is also a heads, then print out a message! if (was_last_coin_heads == TRUE && coin == HEADS) { printf("Two heads in a row -- very lucky!\n"); } was_last_coin_heads = FALSE; if (coin == HEADS) { heads_count += 1; was_last_coin_heads = TRUE; } // coin_count = coin_count + 1; coin_count += 1; } printf("We saw %d heads, out of %d coins\n", heads_count, coin_count); return 0; }