Saturday, September 21, 2019

array insertion deletion using c++


                                                                                                   edited by:: Subhrajit Gorai
                                                                                                                      CMSA 1st sem


source code::



#include<iostream>
#include<conio.h>
using namespace std;

class Array{
                int arr[100],l,e,n;
                public:  
                                void input(){
                                cout<<"Enter the length of the array :";
                                cin>>l;  
                                cout<<"Enter the elements of the array :\n";
                                for(int i=0;i<l;i++)
                                cin>>arr[i];
                                }
                               
                                void output(){
                                cout<<"\nThe elements of the array :\n";
                                for(int i=0;i<l;i++)
                                                cout<<arr[i]<<" , ";
                }
                                               
                void insert_At_Begin(){
                                                cout<<"\nEnter the elements to be inserted in the array :\n";
                                                cin>>e;
                                                l++;
                                                for(int i=l-2;i>=0;i--){
                                                                arr[i+1]=arr[i];
                                                }
                                                arr[0]=e;
                                                output();
                                }
               
                void del_At_Begin(){
                                l--;
                                for(int i=0;i<l;i++)
                                                arr[i]=arr[i+1];
                                                output();
                                }
               
                void insert_At_End(){
                                cout<<"\nEnter the elements to be inserted in the array :\n";
                                cin>>e;
                                l++;
                                arr[l-1]=e;
                                output();
                }
               
                void del_At_End(){
                                                l--;
                                                output();
                }
               
                void insert_At_Index(){
                                                cout<<"Enter the position of the array where the element is to be inserted :";
                                                cin>>n;
                                                while(n>l||n<0){
                                                cout<<"\nINVALID POSITION entered........!!!!";
                                                cout<<"\nEnter the position of the array where the element is to be AGAIN :";
                                                cin>>n;
                                                }
                                                cout<<"\nEnter the elements to be inserted in the array :\n";
                                                cin>>e;
                                                l++;
                                                --n;
                                                for(int i=l-2;i>=n;i--){
                                                                arr[i+1]=arr[i];
                                                }
                                                arr[n]=e;
                                                output();
                                }
               
               
                void del_At_Index(){
                                cout<<"\nEnter the position of the array where the element is to be Deleted :";
                                cin>>n;
                                while(n>l||n<0){
                                                cout<<"\nINVALID POSITION entered........!!!!";
                                                cout<<"\nEnter the position of the array where the element is to be AGAIN :";
                                                cin>>n;
                                }
                                n=n-1;
                                                for(int i=n;i<l;i++)
                                                                arr[i]=arr[i+1];
                                l--;
                                output();
                }
               
                void cho(){
                                int c;
                                cout<<"\n\n  1. Insert Element at the Begining.\n  2. Insert Element at the End.\n  3. Insert Element at any desired position.\n  4. Delete Element at the Begining.\n  5. Delete Element at the End.\n  6. Delete Element at any desired position.\n  7. Exit\n";

                                cout<<"\nEnter the choice:";
                                                cin>>c;
                                                switch (c)
                                                {
                                                case 1:
                                                                insert_At_Begin();
                                                                 break; 
                                                case 2:
                                                                insert_At_End();
                                                                 break; 
                                                case 3:
                                                                insert_At_Index();
                                                                 break; 
                                                case 4:
                                                                del_At_Begin();
                                                                 break; 
                                                case 5: 
                                                                del_At_End();
                                                                   break;                               
                                                case 6:
                                                                del_At_Index();
                                                                 break; 
                                                case 7: exit(0);                  
                                                default :cout<<" Wrong Choice!!!!\n Enter the choice Again.... ";
                                                                                 cho();
                                                }
                }
};
                int main(){
                                int s=0;
                                Array ob;
                                ob.input();
                                ob.output();
                                for(int k=0;;k++)
                                ob.cho();
                                getch();
                                return 0;
}                                             





output::