Thursday, 24 November 2016

Array opertions in C++

//C++ Program to Perform Array opertion ,create,delete,insert

#include<iostream.h>
#include<conio.h>
class operation
{
 int n,a[12];
 public:
  void create();
  void insert(int p,int ele);
  void deletion(int p);
  void disp();
};
 void operation::create()
  {
   int i;
   cout<<"\t Enter the number of element : ";
   cin>>n;
   cout<<"\t Enter the array element  : ";
   for(i=0;i<n;i++)
    cin>>a[i];
  }
 void operation::insert(int p,int ele)
  {
   int i;
   for(i=n-1;i>=p;i--)
  {
   a[i+1]=a[i];
  }
   a[p]=ele;
   n=n+1;
  }
 void operation::deletion(int p)
  {
   int i,ele;
   ele=a[p];
   for(i=p+1;i<n;i++)
   {
    a[i-1]=a[i];
   }
   n=n-1;
   cout<<"deleted element : "<<ele<<endl;
  }
 void operation::disp()
  {
   int i,ele;
   cout<<"Given array is : "<<endl;
   for(i=0;i<n;i++)
   cout<<"\t"<<a[i]<<endl;
  }
 int main()
 {
  int b,n;
  clrscr();
  cout<<"Array  operation "<<endl;
  cout<<"~~~~~  ~~~~~~~~~ "<<endl;
  do
  {
   operation a;
  int ch ,p ,ele;
  cout<<" \t 1.create  "<<endl;
  cout<<" \t 2.Insert"<<endl;
  cout<<" \t 3.Delete"<<endl;
  cout<<"\t Enter the choice            : ";
  cin>>ch;
 switch(ch)
   {
     case 1:
      a.create();
      break;
    case 2:
     cout<<" Enter the position       : ";
     cin>>p;
     cout<<" Enter the insert element : ";
     cin>>ele;
     cout<<"Before insertion"<<endl;
     cout<<"~~~~~~ ~~~~~~~~~"<<endl;
     a.disp( );
     cout<<"\n After insertion"<<endl;
     cout<<"~~~~~~~~ ~~~~~~~~~"<<endl;
     a.insert(p,ele);
     a.disp();
    break;
    case 3:
     cout<<"Enter the position        : ";
     cin>>p;
     cout<<"Before deletion"<<endl;
     cout<<" ~~~~~~ ~~~~~~~~"<<endl;
     a.disp();
     cout<<"\n After Deletion"<<endl;
     cout<<" ~~~~~ ~~~~~~~~"<<endl;
     a.deletion(p);
     a.disp();
     break;
    }
     cout<<" press 1 to continue: ";
     cin>>b;
   }while(b!=0);
 getch();

 }

No comments:

Post a Comment