Question
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".
Explanation / Answer
#include #include using namespace std; int main(){ string pastWord=""; string currentWord,nextWord; int n,t; int singleton=0; int consecutive=0; coutnextWord; 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