2012-10-28, 07:58 PM
Stuck on another problem:
"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 exits the loop it should print the number of consecutive duplicates encountered. In the above case, that value would be 3."
My original idea was to have:
But it doesn't work, since it seems to analyzes groups of 2 rather than consecutive integers. So, how do I analyze consecutive integers/strings/characters using only the while loop?
"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 exits the loop it should print the number of consecutive duplicates encountered. In the above case, that value would be 3."
My original idea was to have:
Code:
dupecount = 0;
while (cin << n) {
int prevNum = n;
while (cin << n) {
if (prevNum == n) {
++dupecount;
}
}
}
cout << dupecount;But it doesn't work, since it seems to analyzes groups of 2 rather than consecutive integers. So, how do I analyze consecutive integers/strings/characters using only the while loop?

