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".
.
.
Click on the title for the solution
.
.
string currentWord="";
string previousWord="";
int currentState=0;
int lastState=-1;
t=-1;
while (currentWord != "xxxxx")
{
cin >> currentWord;
if (currentWord!=previousWord && currentWord!="xxxxx")
currentState=0;
if (currentWord==previousWord && currentWord!="xxxxx")
currentState=1;
if (currentState!=lastState && lastState!=1)
t++;
previousWord=currentWord;
lastState=currentState;
}
cout << t;
.
Click on the title for the solution
.
.
This is the answer:
:
string currentWord="";
string previousWord="";
int currentState=0;
int lastState=-1;
t=-1;
while (currentWord != "xxxxx")
{
cin >> currentWord;
if (currentWord!=previousWord && currentWord!="xxxxx")
currentState=0;
if (currentWord==previousWord && currentWord!="xxxxx")
currentState=1;
if (currentState!=lastState && lastState!=1)
t++;
previousWord=currentWord;
lastState=currentState;
}
cout << t;
do not understand what cin means or what cout means!!
ReplyDelete