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; }
Operations
In mathematics and computer science, an operation is an event that is carried out to satisfy a given task. Basic operations of a computer system are input, processing, output, storage, and control.
Basic Operators
An operator is a symbol that indicates an operation to be performed. We are familiar with operators in mathematics; operators used in computer programming are—in many ways—similar to mathematical operators.
Division Operator
We all learnt about division—and the division operator—in school. You probably know of both these symbols as representing division:
Modulus Operator
Modulus can be represented either as (mod or modulo) in computing operation. Modulus comes under arithmetic operations. Any number or variable which produces absolute value is modulus functionality. Magnitude of any function is totally changed by modulo operator as it changes even negative value to positive.
Operators
In the realm of programming, operators refer to the symbols that perform some function. They are tasked with instructing the compiler on the type of action that needs to be performed on the values passed as operands. Operators can be used in mathematical formulas and equations. In programming languages like Python, C, and Java, a variety of operators are defined.
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;
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 3 images