Question

Can anyone solve this computer science problem involving loops?

Consider this data sequence: "fish bird reptile reptile bird bird bird mammal fish". There is a CONSECUTIVE REPETITION of length 3 (the three consecutive birds) and aCONSECUTIVE REPETITION of length 2 (the two reptiles). There are also several SINGLETONs of length 1 (a singleton fish, a singleton bird, a singleton mammal, andanother singleton fish).

Write some code that uses a loop to reads in a sequence of words, terminated by the "xxxxx". The code assigns to t the number of CONSECUTIVE REPETITIONS that wereread. (For example, in the above data sequence that value would be 2.) Assume that t has already been declared but not initialized. Assume that there will be at leastone word before the terminating "xxxxx".

0 0
Add a comment Improve this question Transcribed image text
Answer #1
I suppose your variable "q" is the counter variable.
It is a little bit confusing, because you didn't initiate the "a" and "d".
May be your code should be like this:

r := a
q := 0
while q < 100 // while q is below 10, the loop will be running and keep running
begin
r := r - d
q := q + 1 // it's important to increase or decrease the "counter variable". So it will not stuck in neverending loop
end
answered by: norma
Add a comment
Answer #2

t = 0;
string prev = "xxxxx", current = "", next, last;
cin >> current;
while (current != "xxxxx") {
cin >> next;
    if ((prev == current || current == next) && current != last) {
        t++;
        last = current;
    }   
    prev = current;
    current = next;
}
cout << t;

Add a comment
Know the answer?
Add Answer to:
Can anyone solve this computer science problem involving loops?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT