Write program to Delete an Element from an Array at Valid Position
#include<stdio.h>
#include<stdlib.h>
#define MAX 5
int a[MAX], pos,
elem;
int n = 0;
void create();
void display();
void delete();
void main()
{
int
i;
printf("\nEnter
the number of elements: ");
scanf("%d",
&n);
if(n!=0)
printf("\nEnter
the elements: ");
for(i=0;
i<n; i++)
{
scanf("%d",
&a[i]);
}
delete();
display();
}
void display()
{
int
i;
if(n
== 0)
{
printf("\nNo
elements to display");
return;
}
printf("\nArray
elements are: ");
for(i=0;
i<n;i++)
printf("%d\t
", a[i]);
}
void delete()
{
int
i;
if(n
== 0)
{
printf("\nArray
is empty and no elements to delete");
return;
}
do
{
printf("\nEnter
a valid position from where element to be deleted: ");
scanf("%d",
&pos);
}while(pos>=n);
elem = a[pos];
printf("\nDeleted element
is : %d \n", elem);
for(
i = pos; i< n-1; i++)
{
a[i]
= a[i+1];
}
n = n-1;
}
Output:
Case 1:
Enter the number
of elements: 0
Array is empty and
no elements to delete
No elements to
display
Case 2:
Enter the number
of elements: 3
Enter the
elements: 11 12 13
Enter a valid
position from where element to be deleted:
6
Enter a valid
position from where element to be deleted:
1
Deleted element is : 12
Array
elements are: 11 13