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<stdio.h>
#define MAX 10
void main()
{
int p,i,a[MAX],n=0,num;
char ch;
clrscr();
printf(“Inserting elements in the array:”);
do
{
printf(“Enter the element: “);
scanf(“%d”,&num);
a[n]=num;
n++;
if(n>=MAX)
{
printf(“!!!!!!Limit exceeded!!!!!!”);
break;
}
fflush(stdin);
printf(“Do you want to continue?? “);
scanf(“%c”,&ch);
}while(ch==’y’);
printf(“\nNow elements in the array are:\n”);
for(i=0;i<n;i++)
{
printf(“\t%d”,a[i]);
}
//DELETING ELEMENT FROM A GIVEN POSITION
printf(“\nDo you want to delete element from the array?? “);
fflush(stdin);
scanf(“%c”,&ch);
if(ch==’y’||ch==’Y’)
{
do
{
printf(“Enter the position of the element to be deleted: “);
scanf(“%d”,&p);
if(p<=n && p>=0)
{
for(i=p-1;i<n;i++)
{
a[i]=a[i+1];
}
n–;
if(n<1)
{
printf(“!!Array is empty now!!”);
break;
}
else
{
printf(“After deletion elements of the array are:\n”);
for(i=0;i<n;i++)
{
printf(“\t%d”,a[i]);
}
}
}
else
printf(“!!Enter a valid position!!”);
printf(“Do you want to continue?? “);
fflush(stdin);
scanf(“%c”,&ch);
}while(ch==’y’||ch==’Y’);
if(n>0)
{
printf(“Remaining elements in the array are:\n”);
for(i=0;i<n;i++)
{
printf(“\t%d”,a[i]);
}
}
}
else
printf(“!!Press any key to exit!!”);
getch();
}

Leave a Reply

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