Respuesta :
Answer:
int fNumber,scndNumber = -1,
dup = 0;
do {
cin >> fNumber;
if ( scndNumber == -1) {
scndNumber = fNumber;
}
else {
if ( scndNumber == fNumber )
duplicates++;
else
scndNumber = fNumber;
}
} while(fNumber > 0 );
cout << dup;
Explanation:
Here three variables are declared to hold the first number which is used obtain all the inputs given by the user, second number to hold the value of last encountered number and “dup” variable to count the number of duplicate values.
“Do-while” loop help us to get the input check whether it is same as previous input if yes then it adds to the duplicate value otherwise the new previous value if stored.
Answer:
int firstNumber,secondNumber = -1, duplicates = 0;
do {
cin >> firstNumber;
if ( secondNumber == -1) {
secondNumber = firstNumber;
}else {
if ( secondNumber == firstNumber )
duplicates++;
else
secondNumber = firstNumber;
}
} while(firstNumber > 0 );
cout << duplicates;
Explanation:
Write some code that uses a loop to read such a sequence of non-negative integers, terminated by a negative number. When the code exits the loop it should print the number of consecutive duplicates encountered. In the above case, that value would be 3. We test the following on a C++ platform.
int firstNumber,secondNumber = -1, duplicates = 0;
do {
cin >> firstNumber;
if ( secondNumber == -1) {
secondNumber = firstNumber;
}else {
if ( secondNumber == firstNumber )
duplicates++;
else
secondNumber = firstNumber;
}
} while(firstNumber > 0 );
cout << duplicates;