The program below simulates tossing a fair coin N times. The program computes the longest sequence of consecutive heads. Or it should compute the longest sequence of consecutive heads. It does not compute the longest sequence of consecutive because one statement is missing and one statement is in the wrong place in the program. Supply the missing statement and move the out of place statement to the right position in the program. If you toss the coin a hundred times the longest sequence of consecutive heads is usually 5 or 6.   #include #include #include #include using namespace std; int main() {     const int HEADS = 0;     const int TAILS = 1;     int seed = (int)time(nullptr);     default_random_engine e(seed);     uniform_int_distribution u(HEADS, TAILS);     int prevCoin = -1;     int N = 100;     int maxlength = 0;     int length = 0;     for (int n = 1; n <= N; ++n)     {         if (coin == HEADS)         {             if (prevCoin == coin)                 length++;             else                 length = 1;         }         else         {             if (length > maxlength)                 maxlength = length;             prevCoin = coin;         }     }     if (length > maxlength)         maxlength = length;     cout << "Maxlength = " << maxlength << endl; }

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter5: Repetition Statements
Section5.5: A Closer Look: Loop Programming Techniques
Problem 15E: (Program) Write a program to simulate the rolling of two dice. If the total of the two dice is 7 or...
icon
Related questions
icon
Concept explainers
Question

The program below simulates tossing a fair coin N times. The program computes the longest sequence of consecutive heads. Or it should compute the longest sequence of consecutive heads. It does not compute the longest sequence of consecutive because one statement is missing and one statement is in the wrong place in the program. Supply the missing statement and move the out of place statement to the right position in the program.

If you toss the coin a hundred times the longest sequence of consecutive heads is usually 5 or 6.

 

#include <ctime>
#include <iostream>
#include <random>
#include <string>
using namespace std;

int main()
{
    const int HEADS = 0;
    const int TAILS = 1;
    int seed = (int)time(nullptr);
    default_random_engine e(seed);
    uniform_int_distribution<int> u(HEADS, TAILS);
    int prevCoin = -1;
    int N = 100;
    int maxlength = 0;
    int length = 0;
    for (int n = 1; n <= N; ++n)
    {
        if (coin == HEADS)
        {
            if (prevCoin == coin)
                length++;
            else
                length = 1;
        }
        else
        {
            if (length > maxlength)
                maxlength = length;
            prevCoin = coin;
        }
    }
    if (length > maxlength)
        maxlength = length;

    cout << "Maxlength = " << maxlength << endl;


}

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 3 images

Blurred answer
Knowledge Booster
Operators
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr