PROGRAM TO DELETE AN ELEMENT FROM Kth POSITION IN THE ARRAY

/*PROGRAM TO DELETE AN ELEMENT FROM Kth POSITION IN THE ARRAY*/
#include<conio.h>
#include<iostream.h>
#define MAX 10
void main()
{
int p,i,a[MAX],n=0,num;
char ch;
clrscr();
cout<<“Inserting elements in the array:”<<endl;
do
{
cout<<“Enter the element: “;
cin>>num;
a[n]=num;
n++;
if(n>=MAX)
{
cout<<“!!!!!!Limit exceeded!!!!!!”;
break;
}
cout<<“Do you want to continue?? “;
cin>>ch;
}while(ch==’y’);
cout<<“Now elements in the array are:”<<endl;
for(i=0;i<n;i++)
{
cout<<a[i]<<“\t”;
}
//DELETING ELEMENT FROM A GIVEN POSITION
cout<<endl<<“Do you want to delete element from the array?? “;
cin>>ch;
if(ch==’y’||ch==’Y’)
{
do
{
cout<<“Enter the position of the element to be deleted: “;
cin>>p;
if(p<=n && p>=0)
{
for(i=p-1;i<n;i++)
{
a[i]=a[i+1];
}
n–;
if(n<1)
{
cout<<“!!Array is empty now!!”;
break;
}
else
{
cout<<“After deletion elements of the array are:”<<endl;
for(i=0;i<n;i++)
{
cout<<a[i]<<“\t”;
}
}
}
else
cout<<“!!Enter a valid position!!”;
cout<<endl<<“Do you want to continue?? “;
cin>>ch;
}while(ch==’y’||ch==’Y’);
if(n>0)
{
cout<<“Remaining elements in the array are:”<<endl;
for(i=0;i<n;i++)
{
cout<<a[i]<<“\t”;
}
}
}
else
cout<<“!!Press any key to exit!!”;
getch();
}

Leave a Reply

Your email address will not be published. Required fields are marked *