183k views
2 votes
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 a CONSECUTIVE REPETITION of length 2 (the two reptiles). There are also several SINGLETONs of length 1 (a singleton fish, a singleton bird, a singleton mammal, and another 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 were read. (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 least one word before the terminating "xxxxx".

User GenericHCU
by
5.7k points

1 Answer

3 votes

Answer:

The following code is written from C++ Programming Language.

num = 0; //integer type variable is initialized

string pre = "xxxxx", current, next; // three string type variable is initialized

cin >> current; //get input from user

while (current != "xxxxx") { //set while loop

cin >> next; //get input from the user

if (pre != current && current != next) { //set if statement

num++;

}

//swap the values.

pre = current;

current = next;

}

Step-by-step explanation:

Here, we initialized an integer type variable "num" to 0.

Then, we initialized the three string type variable "pre" to "xxxxx", "current", "next".

Then, we get input from the user in "current" and then, we set the while loop then, we get input from the user in "next".

Then, we set the if statement and increment the num variable by 1.

After all, we swap the variables.

User Martinwguy
by
6.3k points