PROGRAM TO INSERT AN ELEMENT AT POSITION K IN THE ARRAY

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

Leave a Reply

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