Delete integer from particular position from the array
C program to delete integer from particular position from the array.
Program
#include<stdio.h>
#include<conio.h>
#define MAX 50
void read_array(int a[MAX], int n)
{
int i;
for(i=0;i<n;i++)
{
printf("Element %d\t:",i+1);
scanf("%d",&a[i]);
}
}
void print_array(int a[MAX], int n)
{
int i;
for(i=0;i<n;i++)
{
printf("Element %d\t:%d\n",i+1,a[i]);
}
}
void delete(int a[MAX], int n, int pos)
{
int i;
if(pos >= n+1)
{
printf("\nDeletion at position %d is not possible",pos);
}
else
{
for(i=pos-1;i<n-1;i++)
{
a[i] = a[i+1];
}
printf("\nModified array is\n");
print_array(a, n-1);
}
}
void main()
{
int arr[MAX];
int i, n, pos;
printf("Enter the number of elements in the array\t:");
scanf("%d",&n);
printf("Enter the elements of the array\n");
read_array(arr, n);
printf("Enter the position at which element is to be deleted\t:");
scanf("%d",&pos);
delete(arr, n, pos);
getch();
}
Output
Enter the number of elements in the array :8
Enter the elements of the array
Element 1 :12
Element 2 :25
Element 3 :23
Element 4 :65
Element 5 :25
Element 6 :45
Element 7 :78
Element 8 :54
Enter the position at which element is to be deleted :3
Modified array is
Element 1 :12
Element 2 :25
Element 3 :65
Element 4 :25
Element 5 :45
Element 6 :78
Element 7 :54