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 read 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".

1 Answer

5 votes

Answer:

The code is given below. Follow the question and the code definitions for better understanding.

Step-by-step explanation:

#include<iostream>

#include<string>

using namespace std;

int main(){

string pastWord="";

string currentWord,nextWord;

int n,t;

int singleton=0;

int consecutive=0;

cout<<"Enter words. ('xxxxx' to exit):\\";

cin>>nextWord;

do{

currentWord=nextWord;

cin>>nextWord;

if ( (currentWord!=pastWord)&&(currentWord!=nextWord) )

singleton++;

else if((currentWord==pastWord)&&(currentWord!=nextWord))

consecutive++;

pastWord=currentWord;

}while(nextWord!="xxxxx");

n=singleton;

t=consecutive;

cout<<"There were "<<n<<" singletons and "<<t<<" consecutive repetitions.";

cin.get();

return 0;

}

User Stjohnroe
by
5.0k points