Sunday 27 January 2013

Write a program to insert and deletion of an element at/from Kth position in an array


#include<iostream.h>
 #include<conio.h>
 void main()
 {
 int n,a[20],i,item,j,loc;
 clrscr();
 cout<<"enter the size of array:-"<<endl;
 cin>>n;
 cout<<"\nEnter the elements of the array:-"<<endl;
 for(i=1;i<=n;i++)
            cin>>a[i];
 cout<<"\nEnter the element :-"<<endl;
 cin>>item;
 cout<<"Enter the location where u want to insert:-"<<endl;
 cin>>loc;
 for(j=n;j>=loc;j--)
             {
              a[j+1]=a[j];
             }
  n=n+1;
  a[loc]=item;
  cout<<"After insertion of a element:-"<<endl;
  for(i=1;i<=n;i++)
            {
  cout<<"\n"<<a[i];
              }
            cout<<"Enter the location where from where u want to delete:-"<<endl;
 cin>>loc;
 item=a[loc];
 for(j=loc;j<n;j++)
    {
     a[j]=a[j+1];
    }
  n=n-1;
  cout<<"After Deletion of a element:-"<<endl;
 for(i=1;i<=n;i++)
  cout<<"\n"<<a[i];

getch();
}

No comments:

Post a Comment