Program to delete an element in an array :
#include<iostream.h>
#include<iostream.h>
#include<process.h>
int lsearch(int [], int, int);
void main()
{
int ar[50], item,
n ,index;
cout<<"how many elements do u want
to create array with?...";
cin>>n;
cout<<"\n enter array
elements...\n";
for(int i=0; i<n; i++)
cin>>ar[i];
char ch='y';
while( ch=='y'|| ch=='Y')
{
cout<<"\n
enter element to be deleted...";
cin>> item;
if(n==0)
{
cout<<"underflow!!\n";exit(1); }
cout<<"underflow!!\n";exit(1); }
index=
lsearch(ar, n, item);
if(index!=-1)
ar[index]= 0;
else
cout<<"
sorry!! no such element in the array.\n";
cout<<"\n the array now is as shown
below....\n";
cout<<" zero (0) signifies deleted
element \n";
for(i=0;
i<n; i++)
cout<< ar[i]<<" ";
cout<<endl;
cout<<"
after this emptied space will be shifted to the end of array \n”;
for (i=index; i<n; i++)
{
ar[i]=ar[i+1]; }
n-=1;
cout<<"\n
want to delete more elements? (y/n)...";
cin>>ch;
}
cout<<" the array after
shifting all emptied spaces towards right is..\n”:
for
(i=0; i<n; i++)
cout<<
ar[i]<<" ";
cout<<endl;
}
int lsearch(int ar[], int size, int item)
{
for(int i=0; i<size; i++)
{
if(ar[i]==item)
return I;
}
return -1;
}
0 Comments