Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You are pleasantly minding your own business when your roommate suddenly asks yo

ID: 3919007 • Letter: Y

Question

You are pleasantly minding your own business when your roommate suddenly asks you: “Say, room- mate, I have this sequence 1, 11, 21, 1211, 111221, .... What say you to finding the next integer?” After beating your brain for twenty minutes in a feeble attempt to find the pattern, it is revealed to you: given element (say) i3 = 21, the next element is formed from the phrase “one 2, one 1”. In general, in+1 is formed by reading in left-to-right as a string of digits and, for each run of n instances of digit d, append n followed by d to the output digit-string. For example, 114421 contains two 1s, two 4s, one 2, and one 1; the next integer in the sequence would be 21241211.

Eager to put your programming prowess to use, you set about formalizing this transformation in a small utility program. The program should take (read from a file) a count n on one line, followed by n integers; for each of the n integers, it should output the integer that comes after n in the sequence.

Sample Input 5 // five integers follow
21
114421  
5555555555
123456  
1
Sample Output
1211  
21241211  
105  
111213141516  
11

Explanation / Answer

#include<iostream>

using namespace std;

int main()

{

long k;

string n;

int c=0,i=0;

char p;

cout<<"number of inputs:";

cin>>k;

while(k>0)

{

cin>>n;//reading string of digits

c=1;

p= n[0];

i=1;

//calculating

//printing output..

while(n[i]!='')

{

if(n[i]==p)

c++;

else

{

cout<<c<<p;

c=1;

p=n[i];

}

i++;

}

cout<<c<<p;

cout<<" ---------------- ";

k--;

}

return 0;

}

output:

number of inputs:5
21
1211
----------------
114421
21241211
----------------
5555555555
105
----------------
123456
111213141516
----------------
1
11
----------------


Process exited normally.
Press any key to continue . . .

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote