Consider this data sequence: "fish bird reptile reptile bird bird bird mammal fish". Let's define a SINGLETON to be a data element that is not repeated immediately before or after itself in the sequence. So, here there are four SINGLETONs (the first appearance of "fish", the first appearance of "bird", "mammal", and the second appearance of "fish"). Write some code that uses a loop to read a sequence of words, terminated by the "xxxxx". The code assigns to the variable n the number of SINGLETONs that were read. (For example in the above data sequence it would assign 4 to n. Assume that n has already been declared. but not initialized. Assume that there will be at least one word before the terminating "xxxxx". ASSUME the availability of a variable, stdin, that references a Scanner object associated with standard input.

Respuesta :

Answer:

// See attachment for full source code and output

// Program was implemented using C++ Programming Language

// Comments are used for explanatory purpose

#include<iostream>

using namespace std;

int main()

{

int n = 0; // Declare and initialise an integer variable n to 0

string prev ="xxxxx", current, next; // Declare string variables prev, current and next.

// At the same time initialise variable string to xxxxx

cin>>current; // Accept input for variable current

//If current is not equal to prev, the following statement in between the loop will be executed

// Else the program is terminated

while(current!=prev)

{

cin>>next; // accept input for variable next

if(prev != current && current != next)

{

n++; // Increment n by 1

}

prev = current; //store the value of current in previous

current = next; // store the value of next in current

cout<<prev<<endl; // Print previous string

cout<<current<<endl; // Print current string

cout<<next<<endl; // Print next string

}

return 0;

}

Ver imagen MrRoyal
Ver imagen MrRoyal