A program to declare an array of 10 elements, which does the following operations according to the user:
a) Deletes an element at the beginning of array.
b) Deletes an element at the end of array.
c) Deletes an element at the desired position in the array.
The program has three different functions begin(), end() and pos() for doing the various functions. The array is entered in the main program and is passed as call by reference parameter for doing the various operations.
Note: The program continues till the user wants
Assumption: The deleted element is represented by 0, which is placed at the end of the array and the value of no element in the array is element is originally 0.

 

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

int a[10],i,x=1,y=9,l=9,m=9;

void begin(int b[])
{
for(i=0;a[i]!=0;i++)
b[i]=b[i+1];
b[l]=0;
l--;
}

void end(int b[])
{
b[l]=0;
l--;
}

void position(int b[])
{
int p;
cout<<"\nEnter the position where element has to be deleted: ";
cin>>p;
if(b[p]!=0)
{
for(i=p-1;a[i]!=0;i++)
b[i]=b[i+1];
b[m]=0;
m--;
}
else
cout<<"\nElement has been deleted."<<endl;
}

void display(int b[])
{
cout<<"\nArray: ";
for(i=0;i<10;i++)
cout<<b[i]<<" ";
cout<<endl;
}

void main()
{
 clrscr();
 cout<<"Enter elements of array: "<<endl;

 for(i=0;i<10;i++) 
  {
   cout<<"Enter element no."<<i+1<<": ";
   cin>>a[i];
  }

 clrscr();

 int ch;

 do
  {
   cout<<"1.Delete an element at the begining."<<endl;
   cout<<"2.Delete an element at the end."<<endl;
   cout<<"3.Delete an element at the desired position."<<endl;
   cout<<"4.Display array."<<endl;
   cout<<"5.Exit"<<endl;
   cout<<"Enter your choice: ";
   cin>>ch;
   switch(ch)
    {
     case 1: {
                 begin(a);
                 break;
                }
     case 2: {
                 end(a);
                 break;
                }
     case 3: {
                 position(a);
                 break;
                }
     case 4: {
                 display(a);
                 break;
                }
     case 5: {    
                 break;
                }
    default: {
                 cout<<"\nWrong choice entered."<<endl;
                 break;
                }
    }
   cout<<endl;
  }
 while(ch!=5);
}

 

< Back