Menu driven program to insert or delete a specific element in a sorted array
Wednesday, March 04, 2020//menu driven program to insert or delete a specific element in a sorted array
#include<iostream.h>
#include<conio.h>
#include<process.h>
void insert(int A[20],int& N,int ELEMENT)
{ int pos;
if(N==20)
{ cout<<"\n Overflow";
}
if(A[0]>ELEMENT)
pos=0;
if(A[N-1]<ELEMENT)
pos=N;
for(int i=0;i<N;i++)
{ if(A[i]<ELEMENT&&A[i+1]>ELEMENT)
pos=i+1;
}
for(i=N-1;i>=pos;i--)
A[i+1]=A[i];
A[pos]=ELEMENT;
N++;
cout<<"\n Array after insertion is:";
for(i=0;i<N;i++)
cout<<A[i]<<"\t";
}
void delet(int A[20],int N,int ELEMENT)
{ int pos;
if(N==0)
{ cout<<"\n Underflow";
}
else
{ for(int i=0;i<N;i++)
{ if(A[i]==ELEMENT)
pos=i;
}
for(i=pos;i<N;i++)
A[i]=A[i+1];
N--;
cout<<"\n Array after deletion is:";
for(i=0;i<N;i++)
cout<<A[i]<<"\t";
}
}
void main()
{ clrscr();
int a[20],n=0,element,p;
l:
cout<<"\n Press 1 for INSRETION"
<<"\n Press 2 for DELETION"
<<"\n Press 3 for EXIT\n";
cout<<"\n Enter your choice:";
cin>>p;
if(p==3)
{ clrscr();
cout<<"\n\t\t THANK YOU";
getch();
exit(0);
}
else if(p==1)
{ clrscr();
cout<<"\n Enter the element to be inserted:";
cin>>element;
insert(a,n,element);
goto l;
}
else
{ clrscr();
cout<<"\n Enter the element to be deleted:";
cin>>element;
delet(a,n,element);
goto l;
}
getch();
}
#include<iostream.h>
#include<conio.h>
#include<process.h>
void insert(int A[20],int& N,int ELEMENT)
{ int pos;
if(N==20)
{ cout<<"\n Overflow";
}
if(A[0]>ELEMENT)
pos=0;
if(A[N-1]<ELEMENT)
pos=N;
for(int i=0;i<N;i++)
{ if(A[i]<ELEMENT&&A[i+1]>ELEMENT)
pos=i+1;
}
for(i=N-1;i>=pos;i--)
A[i+1]=A[i];
A[pos]=ELEMENT;
N++;
cout<<"\n Array after insertion is:";
for(i=0;i<N;i++)
cout<<A[i]<<"\t";
}
void delet(int A[20],int N,int ELEMENT)
{ int pos;
if(N==0)
{ cout<<"\n Underflow";
}
else
{ for(int i=0;i<N;i++)
{ if(A[i]==ELEMENT)
pos=i;
}
for(i=pos;i<N;i++)
A[i]=A[i+1];
N--;
cout<<"\n Array after deletion is:";
for(i=0;i<N;i++)
cout<<A[i]<<"\t";
}
}
void main()
{ clrscr();
int a[20],n=0,element,p;
l:
cout<<"\n Press 1 for INSRETION"
<<"\n Press 2 for DELETION"
<<"\n Press 3 for EXIT\n";
cout<<"\n Enter your choice:";
cin>>p;
if(p==3)
{ clrscr();
cout<<"\n\t\t THANK YOU";
getch();
exit(0);
}
else if(p==1)
{ clrscr();
cout<<"\n Enter the element to be inserted:";
cin>>element;
insert(a,n,element);
goto l;
}
else
{ clrscr();
cout<<"\n Enter the element to be deleted:";
cin>>element;
delet(a,n,element);
goto l;
}
getch();
}
0 Comments