Saturday, March 22, 2014

GO BACK N PROTOCOL PROGRAM IN C++

# include <iostream.h>
# include <conio.h>
# include <stdlib.h>
# include <time.h>
# include <math.h>

# define TOT_FRAMES 500
# define FRAMES_SEND 10

class gobkn
{
 private:
  int fr_send_at_instance;
  int arr[TOT_FRAMES];
  int arr1[FRAMES_SEND];
  int sw;
  int rw; // tells expected frame
 public:
  gobkn();
  void input();
  void sender(int);
  void reciever(int);
};

gobkn :: gobkn()
{
 sw = 0;
 rw = 0;
}

void gobkn :: input()
{
 int n;  // no of bits for the frame
 int m;  // no of frames from n bits

 cout << "Enter the no of bits for the sequence no ";
 cin >> n;

 m = pow (2 , n);

 int t = 0;

 fr_send_at_instance = (m / 2);

 for (int i = 0 ; i < TOT_FRAMES ; i++)
 {
  arr[i] = t;
  t = (t + 1) % m;
 }

 sender(m);
}

void gobkn :: sender(int m)
{
 int j = 0;

 for (int i = sw ; i < sw + fr_send_at_instance ; i++)
 {
  arr1[j] = arr[i];
  j++;
 }

 for (i = 0 ; i < j ; i++)
  cout << " SENDER   : Frame " << arr1[i] << " is sent\n";

 reciever (m);
}

void gobkn :: reciever(int m)
{
 time_t t;
 int f;
 int f1;
 int a1;
 char ch;

 srand((unsigned) time(&t));
 f = rand() % 10;

   // if = 5 frame is discarded for some reason
   // else they are correctly recieved

 if (f != 5)
 {
  for (int i = 0 ; i < fr_send_at_instance ; i++)
  {
   if (rw == arr1[i])
   {
    cout << "RECIEVER : Frame " << arr1[i] << " recieved correctly\n";
    rw = (rw + 1) % m;
   }
   else
    cout << "RECIEVER : Duplicate frame " << arr1[i] << " discarded\n";
  }
  a1 = rand() % 15;

   // if a1 belongs to 0 to 3 then
   //     all ack after this (incl this one) lost
   // else
   //     all recieved

  if (a1 >= 0 && a1 <= 3)
  {
   cout << "(Acknowledgement " << arr1[a1] << " & all after this lost)\n";
   sw = arr1[a1];
  }
  else
   sw = (sw + fr_send_at_instance) % m;
 }
 else
 {
  f1 = rand() % fr_send_at_instance;

   // f1 gives index of the frame being lost

  for (int i = 0 ; i < f1 ; i++)
  {
   if (rw == arr1[i])
   {
    cout << " RECIEVER : Frame " << arr1[i] << " recieved correctly\n";
    rw = (rw + 1) % m;
   }
   else
    cout << " RECIEVER : Duplicate frame " << arr1[i] << " discarded\n";
  }

  int ld = rand() % 2;
    // ld == 0 frame damaged
    // else frame lost
  if (ld == 0)
   cout << " RECIEVER : Frame " << arr1[f1] << " damaged\n";
  else
   cout << "            (Frame " << arr1[f1] << " lost)\n";

  for (i = f1 + 1 ; i < fr_send_at_instance ; i++)
    cout << " RECIEVER : Frame " << arr1[i] << " discarded\n";

  cout << " (SENDER TIMEOUTS --> RESEND THE FRAME)\n";

  sw = arr1[f1];
 }
 cout << "Want to continue...";
 cin >> ch;

 if (ch == 'y')
  sender(m);
 else
  exit(0);
}

void main()
{
 clrscr();
 gobkn gb;
 gb.input();
 getch();
}

/*   OUTPUT

Enter the no of bits for the sequence no 4
 SENDER   : Frame 0 is sent
 SENDER   : Frame 1 is sent
 SENDER   : Frame 2 is sent
 SENDER   : Frame 3 is sent
 SENDER   : Frame 4 is sent
 SENDER   : Frame 5 is sent
 SENDER   : Frame 6 is sent
 SENDER   : Frame 7 is sent
RECIEVER : Frame 0 recieved correctly
RECIEVER : Frame 1 recieved correctly
RECIEVER : Frame 2 recieved correctly
RECIEVER : Frame 3 recieved correctly
RECIEVER : Frame 4 recieved correctly
RECIEVER : Frame 5 recieved correctly
RECIEVER : Frame 6 recieved correctly
RECIEVER : Frame 7 recieved correctly
Want to continue...n

*/

DISTANCE VECTOR ROUTING PROGRAM IN C++

// SIMULATE DISTANCE VECTOR ROUTING
// ALWAYS DRY RUN FOR BETTER UNDERSTANDING

# include <iostream.h>
# include <conio.h>
# include <stdio.h>
# include <string.h>

class router
{
 private:
  char name[20];
  int dist[10];

 public:
  void get_r_table (int , int);
  friend void find_table (router *);
};

void find_table (router *);

int total_r;
int adj;
char names[10][20];
char N[20];

void router :: get_r_table (int no , int flag)
{
 if ( flag == 1 )
 {
  cout << "Enter the name of the adjacent router " << no << " : ";
  gets (name);
 }
 else
  strcpy (name , N);

 cout << "Enter the distances of each router from this router :\n";
 cout << "\tROUTER\tDISTANCE\n";
 cout << "\t------\t--------\n";

 for (int i = 0 ; i < total_r ; i++)
 {
  cout << "\t" << names[i] << "\t";
  if ( strcmp ( names[i] , name ) == 0 )
  {
   dist[i] = 0;
   cout << dist[i] << "\n";
  }
  else
   cin >> dist[i];
 }
}

void find_table(router *r)
{
 cout << "Routing table for router " << N << " :-\n";
 cout << "\tRouter\tDistance\n";
 cout << "\t------\t--------\n";

 for ( int i = 0 ; i < total_r ; i++ )
 {
  int temp;
  if ( strcmp (N , names[i] ) == 0 )
   temp = 0;
  else
  {
   temp = r[0].dist[i];
   for ( int j = 1 ; j < adj + 1 ; j++ )
    if ( r[j].dist[i] < temp && r[j].dist[i] != 0 )
     temp = r[j].dist[i] + r[0].dist[j];
  }
  r[0].dist[i] = temp;
 // r[0] contains data of the router whose routing table is to be found out
 }

 for ( i = 0 ; i < total_r ; i++ )
 {
  cout << "\t" << names[i] << "\t";
  cout << r[0].dist[i] << "\n";
 }
}

void main()
{
 router r[10];

 clrscr();

 cout << "Enter the total no of routers in the subnet [MAX 10] :- ";
 cin >> total_r;
 cout << "Enter the names of all the routers :-\n";
 for (int i = 0 ; i < total_r ; i++)
 {
  cout << "\t" << i + 1 << " . ";
  gets (names[i]);
 }
 cout << "Enter the router whose routing table is to be found out : ";
 gets (N);

 cout << "Enter the no of routers adjacent to this router : ";
 cin >> adj;

 int flag = 0;
 for ( i = 0 ; i < adj + 1; i++ )
 {
  r[i].get_r_table (i , flag);
  flag = 1;
 }

 find_table (r);
 getch();
}


/*                 OUTPUT


Enter the total no of routers in the subnet [MAX 10] :- 6
Enter the names of all the routers :-
        1 . A
        2 . B
        3 . C
        4 . D
        5 . E
        6 . F
Enter the router whose routing table is to be found out : C
Enter the no of routers adjacent to this router : 3
Enter the distances of each router from this router :
        ROUTER  DISTANCE
        ------  --------
        A       11
        B       6
        C       0
        D       3
        E       5
        F       6
Enter the name of the adjacent router 1 : B
Enter the distances of each router from this router :
        ROUTER  DISTANCE
        ------  --------
        A       5
        B       0
        C       8
        D       12
        E       6
        F       2
Enter the name of the adjacent router 2 : D
Enter the distances of each router from this router :
        ROUTER  DISTANCE
        ------  --------
        A       16
        B       12
        C       6
        D       0
        E       9
        F       10
Enter the name of the adjacent router 3 : E
Enter the distances of each router from this router :

        ROUTER  DISTANCE
        ------  --------
        A       7
        B       6
        C       3
        D       9
        E       0
        F       1
Routing table for router C :-
        Router  Distance
        ------  --------
        A       10
        B       6
        C       0
        D       3
        E       5
        F       4

*/

Cyclic Redundancy Check (CRC) Program in C++

// SIMULATE CYCLIC REDUNDANCY CHECK (CRC)
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#include<graphics.h>
#include<DOS.h>
int digitinbin(int number)
{  int i=0;
  int n=number;
  while(n!=0)
    { n=n/2;
      i++;
    }
   return i;
}
int bintodec(int *a,int d)
{  int num=0;
   for(int i=0;i<d;i++)
    { if(a[i]==1)
       num=num+pow(2,i);
    }
  return num;
}
int* dectobin(int n)
{ int *A=new int[32];
  int i=0;
  int num=n;
  while(num!=0)
    { A[i]=num%2;
      num=num/2;
      i++;
    }
  return A;
}

int sender(int mess,int poly)
{ int k=0,num1;
  int d1=digitinbin(poly);
  mess=mess*pow(2,d1-1);
  int d2=digitinbin(mess);
  int *B=dectobin(mess);
  int counter=0;
  int times=d2-d1+1;
  int *temp=new int[d1];
  int a=pow(2,d1-1);
  while(counter<times)
   { int j=0;
     if(counter==0)
      {
     for(int i=times-counter-1;i<d2-counter;i++)
      {  temp[j]=B[i];
  j++;
      }
      num1=bintodec(temp,j);
      }
      int num2=poly;
      if(counter!=0)
       num1=k;
     if(num1>=a)
       k=num1^num2;
     else
       k=num1;

     if(d2-d1-counter-1>=0)
     {
     if(B[d2-d1-counter-1]==1)
      k=k*2+1;
     else
      k=k*2;
     }
   counter++;
    }
   return (k^mess);
}
void receiver(int mess,int poly)
{ int k=0,num1;
  int d1=digitinbin(poly);
  int d2=digitinbin(mess);
  int *B=dectobin(mess);
  int counter=0;
  int times=d2-d1+1;
  int *temp=new int[d1];
  int a=pow(2,d1-1);
  while(counter<times)
   { int j=0;
     if(counter==0)
      {
     for(int i=times-counter-1;i<d2-counter;i++)
      {  temp[j]=B[i];
  j++;
      }
      num1=bintodec(temp,j);
      }
      int num2=poly;
      if(counter!=0)
       num1=k;
     if(num1>=a)
       k=num1^num2;
     else
       k=num1;

     if(d2-d1-counter-1>=0)
     {
     if(B[d2-d1-counter-1]==1)
      k=k*2+1;
     else
      k=k*2;
     }
   counter++;
    }
  if(k==0)
    cout<<"MESSAGE TRANSMITTED SUCCESSFULLY\n"<<endl;
  else
    cout<<"ERROR IN TRANSMISSION\n"<<endl;

}
void main()
{ clrscr();
  int message,genpoly,ch=1;
  cout<<"enter the number to be transmitted"<<endl;
  cin>>message;
  cout<<"Enter the generator polynomial number"<<endl;
  cin>>genpoly;
  int gp=sender(message,genpoly);

  while(ch==1 || ch==2)
  {
  cout<<"MENU"<<endl;
  cout<<"1-SUCCESSFUL TRANSMISSION"<<endl;
  cout<<"2-ERROR IN TRANSMISSION"<<endl;
  cout<<"PRESS ANY OTHER KEY TO EXIT"<<endl;
  cout<<"Enter your choice"<<endl;
  cin>>ch;
   if(ch==1)
  { cout<<"TRANSMITTING MESSAGE .......\n"<<endl;
    delay(5000);
    receiver(gp,genpoly);
    int *B=dectobin(message);
    int dig=digitinbin(message);
    cout<<"The transmitted message is ";
      for(int i=dig-1;i>=0;i--)
 cout<<B[i];
    cout<<"\n"<<endl;
  }
   else
   if(ch==2)
  {  cout<<"TRANSMITTING MESSAGE .......\n"<<endl;
    delay(5000);
    receiver(gp+1,genpoly);
     cout<<endl;
   }
}

 cout<<"PROGRAM ENDS"<<endl;
getch();
}

/*     OUTPUT

enter the number to be transmitted
729
Enter the generator polynomial number
19
MENU
1-SUCCESSFUL TRANSMISSION
2-ERROR IN TRANSMISSION
PRESS ANY OTHER KEY TO EXIT
Enter your choice
1
TRANSMITTING MESSAGE .......

MESSAGE TRANSMITTED SUCCESSFULLY

The transmitted message is 1011011001

MENU
1-SUCCESSFUL TRANSMISSION
2-ERROR IN TRANSMISSION
PRESS ANY OTHER KEY TO EXIT
Enter your choice
2
TRANSMITTING MESSAGE .......

ERROR IN TRANSMISSION


MENU
1-SUCCESSFUL TRANSMISSION
2-ERROR IN TRANSMISSION
PRESS ANY OTHER KEY TO EXIT
Enter your choice
4
PROGRAM ENDS

*/

Wednesday, February 19, 2014

Open Ignite TCS Interview

So, If you have been shortlisted for the interview process @ the TCS Open Ignite Interview . They will call you to review your academics .

Each Candidate passes through the following phases to be selected .

1. There will be a general aptitude test for all the candidates .
The total no. of questions will be around 10 out of which 2 questions will be related to programming in any language you wish ( An Easy Program & A tough one ) . 


Then you will be allowed to verify your documents particularly your mark sheets of class Xth , XIIth and your Graduation . They will also ask your one of the open lab solutions .
2. After the verification process the candidate will be send for two rounds of interviews . 

3. 1st Round consists of a Technical Interview ,, they will ask you questions related to programming languages you have learnt in your graduation . 

4. 2nd round will consist of HR Round , in which they particularly ask why you want to join TCS etc.based questions .


And this will end your interview .
The results are mostly declared by two weeks on your mail . 


To more about TCS Ignite, watch this video.


Monday, February 3, 2014



Solutions of the recommended book Morris Mano IIIrd Edition .

Click Here : Download Ebook

Sunday, February 2, 2014