182k views
5 votes
Consider this data sequence: "3 11 5 5 5 2 4 6 6 7 3 -8". Any value that is the same as the immediately preceding value is considered a CONSECUTIVE DUPLICATE. In this example, there are three such consecutive duplicates: the 2nd and 3rd 5s and the second 6. Note that the last 3 is not a consecutive duplicate because it was preceded by a 7. Write some code that uses a loop to read such a sequence of non-negative integers, terminated by a negative number. When the code finishes executing, the number of consecutive duplicates encountered is printed. In this case,3 would be printed.

User Yogamurthy
by
5.7k points

1 Answer

6 votes

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;

Step-by-step explanation:

User Oleg Karasik
by
6.0k points