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;="">=> { 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;} "maxlength=""> "maxlength = " << maxlength << endl;}>
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;="">=> { 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;} "maxlength=""> "maxlength = " << maxlength << endl;
cout < "maxlength = " << maxlength << endl;
}
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here