8. Delete an element of an array.
#include <stdio.h>
#include <conio.h>
int main()
{
int a[20], num, pos, i;
// clrscr();
printf("Enter the total number of the elements of array: ");
scanf("%d", &num);
for (i = 0; i < num; i++) /* scaning the elemtents of the array */
{
printf("Enter %d element of array: ", i + 1);
scanf("%d", &a[i]);
}
printf("\nEnter the position of the element to delete: ");
scanf("%d", &pos);
for (i = pos; i < num; i++) /* overflow of the elements */
a[i - 1] = a[i]; /* delete the position of the element*/
a[num - 1] = 0; /* assign the 0 value to the last position*/
printf("\nAfter deletion %d position of the element the array is", i);
for (i = 0; i < num - 1; i++)
printf("\nElement is %d at %d position", a[i], i + 1);
return 0;
}
Output
Enter the total number of the elements of array: 5
Enter 1 element of array: 2
Enter 2 element of array: 4
Enter 3 element of array: 3
Enter 4 element of array: 5
Enter 5 element of array: 9
Enter the position of the element to delete: 3
After deletion 5 position of the element the array is
Element is 2 at 1 position
Element is 4 at 2 position
Element is 5 at 3 position
Element is 9 at 4 position