Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design a program that plays the following game: one player repeatedly flips a co

ID: 3625550 • Letter: D

Question

Design a program that plays the following game: one player repeatedly flips a coin. If the coin shows heads then the player adds 1 to the heads counter; if the coin shows tails then the player assigns 0 to the heads counter. The game ends as soon as the player flips 3 consecutive heads or flips the coin 8 times without flipping three consecutive heads. Use loops, variables, if statements.

The following incorrect algorithm may help you write the algorithm for the game correctly.

heads counter = 0
coin flip counter = 0
repeat
flip coin
if coin is heads then
add 1 to head’s counter
end if
until head’s counter = 3
display winner message





Explanation / Answer

I wasn't sure how you wanted me to generate the flip so I used rand.

-------------------------------------------------------------------------------

#include <iostream>

#include <time.h>

using namespace std;

void main ()
    {
    time_t Seed;
    int Coin = 0;
    int HeadsCounter = 0;
    int FlipCounter = 0;
    time (&Seed);
    // Makes the random number based on a time seed
    srand ((unsigned) Seed);
    do {
    Coin = (rand () % 2);
    if (Coin == 1)
        cout << "Heads" << endl;
    else
        cout << "Tails" << endl;
    FlipCounter++;
    if (Coin == 1)
        {
        HeadsCounter++;
        }
    else
        {
        HeadsCounter = 0;
        }
        } while ((FlipCounter < 8) && (HeadsCounter < 3));

    if (HeadsCounter == 3)
        cout << "You got three consecutive heads" << endl;
    else
        cout << "You flipped eight times and did not get three consecutive heads" << endl;
    }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote