Friday, 29 June 2012

Insertion In Array At Different Position's Using C++


#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<dos.h>
 int a[15],i,n,item,k,j;
 void main()
 {


   void menu();
   void top(int [],int &);
   void last(int [],int &);
   void kth(int [],int &);
   void display(int [],int &);
   clrscr();
     int a[15],i,n,item;
  clrscr();
   cout<<endl<<"enter the no of elements :";
   cin>>n;
   cout<<endl<<" enter the elements : ";
     for(i=0;i<n;i++)
     {
cout<<endl<<"a["<<i<<"] = ";
cin>>a[i];
     }
     while(1)
    {
       menu();
       int o;
       cout<<endl<<endl<<"ENTER YOUR CHOICE :";
       cin>>o;
       switch(o)
       {
case 1:
top(a,n);
break;
case 2:
last(a,n);
break;
case 3:
kth(a,n);
break;
case 4:
display(a,n);
break;
case 5:
cout<<endl<<endl<<"EXITING. . . .";
delay(1000);
exit(0);
default:
cout<<endl<<endl<<"WRONG CHOICE. . .TRY AGAIN. . ";
getch();
       }
    }
 }


  void menu()
  {
    clrscr();
    cout<<endl<<endl<<"\t\t\t\t=========INSERTION IN ARRAY==========";
    cout<<endl<<"\t\t\t\t1.TOP                               ";
    cout<<endl<<"\t\t\t\t2.LAST";
    cout<<endl<<"\t\t\t\t3.KTH";
    cout<<endl<<"\t\t\t\t4.DISPLAY";
    cout<<endl<<"\t\t\t\t5.EXIT";
  }


   void top(int a[15],int &n)
   {
  n=n+1;
  cout<<endl<<"enter the item :";
  cin>>item;
     for(i=n-1;i>0;i--)
     {
a[i]=a[i-1];
     }
     a[i]=item;


   }


    void last(int a[15],int &n)
    {
  n=n+1;
  cout<<endl<<"enter the item :";
  cin>>item;
     a[n-1]=item;
    }
     void display(int a[15],int &n)
     {
      cout<<endl<<"array after insertion is :";
      for(i=0;i<n;i++)
      {
cout<<endl<<"a["<<i<<"] = "<<a[i];
      }


 getch();
     }
      void kth(int a[15],int &n)
      {
cout<<endl<<"enter the kth pos : ";
     cin>>k;
     cout<<endl<<"enter the item to be inserted at kth pos : ";
     cin>>item;
     n=n+1;
     for(int j=n-1;j>=k;j--)
     {
      a[j]=a[j-1];
     }
      a[k]=item;
      }

No comments:

Post a Comment