Program to generate Look and Say series

Program to generate Look and Say series

This program will Generate the ‘Look and Say’ series for the given no.of lines from the User.

Logic To Generate the Series :

  • Get the No.of lines to print from the User to variable n .
  • Initialise array a with 1 for first iteration .
  • Using array b count the no.of occurences of 1 before any other number and insert the count value to even index of array b and insert the actual number in next position of array b.
  • Continue this until array a reaches its end.
  • Now print the array a values.
  • Then copy array b to array a and repeat the steps for given no.of times from the user .

Program :

#include<iostream>
using namespace std;
int main()
{
   int n;                            //n=no.of lines to print
   cout<<"Enter the Number : ";
   cin>>n;
   int a[100*100*n];                 //a[]=array to print series
   int b[100*100*n];                 //b[]=array to store counts with actual numbers
   a[0]=1;                           //1 is fixed for first time
   int i,j,count,ia,ib;              //count=stores count,ia=index of a[],ib=index of b[]
   ia=1;                             //ia=1 -> first time a[] contains only one element
   
   //iterate for n number of times to print the series 

   for(int m=0;m<n;m++)           
   {
      ib=0;                          //ib=0 -> for every iteration b[] is used to store values from beginning
      count=0;                       //count is made 0 after each line
      
      for(i=0,j=0;j<ia;j++)    //loop which runs upto no.of element in a[]
      {
          if(a[i]==a[j])    //if condition executes if same number is found and increment count of that number
          {
              count++;
          }
          else    //if different number is found else part gets executed
          {
             b[ib]=count;            //at first b[] stores the count
             b[ib+1]=a[j-1];         //next b[] stores the actual number
             ib=ib+2;                //since 2 positions of b[] is used so 2 times ib is incremented
             i=j;                    //now i stores the index of next different number to continue
             j=j-1;                  //j is decremented once to hold the current numbers' position
             count=0;                //count is resetted in order to store the count of next number
          }
      }
      
      for(int k=0;k<ia;k++)    //loop to print a[]
      {
          cout<<a[k];                
      }
      
      b[ib]=count;                   //the last numbers' count in a[] is stored here
      b[ib+1]=a[j-1];                //the last actual number in a[] is stored here
      ib=ib+2;                       //ib is incremented +2
      cout<<"\n";                    //one line of series is printed so go to new line is printed
      
      for(int k=0;k<ib;k++)    //loop to store b[] in a[] to continue next iteration
      {
          a[k]=b[k];                 
      }
      ia=ib;                         //ia now becomes no.of elements of ib
    } 
    return 0;
}

 

Suriya27

A Programmer who have his programming skills on C,C++, Java and also interested in Android Development. He follows the famous quotes by Linus Torvalds ” Talk is cheap, Show me the code “ .