PROGRAM TO INSERT AN ELEMENT AT POSITION K IN THE ARRAY

/*PROGRAM TO INSERT AN ELEMENT AT POSITION K IN THE ARRAY*/
#include<conio.h>
#include<iostream.h>
#define MAX 10
void main()
{
int p,i,a[MAX]={2,3,4,5},n=4,num;
char ch;
clrscr();
cout<<“Currently elements in the array are: “<<endl;
for(i=0;i<=3;i++)
{
cout<<a[i]<<“\t”;
}
cout<<endl<<“Do you want to insert new element?? “;
cin>>ch;
if(ch==’y’)
{
do
{
cout<<“Enter the position of insertion: “;
cin>>p;
if(p<=n+1)
{
cout<<“Enter the element: “;
cin>>num;
for(i=n-1;i>=p-1;i–)
{
a[i+1]=a[i];
}
a[i+1]=num;
n++;
cout<<“Now elements in the array are:”<<endl;
for(i=0;i<n;i++)
{
cout<<a[i]<<“\t”;
}
}
else
cout<<“!!You must specify a consecutive memory location!!”;
if(n>=MAX)
{
cout<<endl<<“!!!!!!Limit exceeded!!!!!!”;
break;
}
cout<<endl<<“Do you want to continue?? “;
cin>>ch;
}while(ch==’y’);
}
else
{
cout<<“!!Press any key to exit!!”;
}
getch();
}

Leave a Reply

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