Array Deletion with C: Write a program to delete an element from the array.
Input and Output Format:Assume that the maximum number of elements in the array is 20.
Refer sample input and output for formatting specifications.
All text in bold corresponds to input and the rest corresponds to output.
Sample Input and Output 1:
Enter the number of elements in the array
5
Enter the elements in the array
1
2
3
4
5
Enter the location where you wish to delete an element
4
Array after deletion is
1
2
3
5
Sample Input and Output 2:
Enter the number of elements in the array
5
Enter the elements in the array
1
2
3
4
5
Enter the location where you wish to delete an element
10
Invalid Input
Solution
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a[20],i,x,n;
printf("Enter the number of elements in the array\n");
scanf("%d",&n);
if(n<=20)
{
printf("Enter the elements in the array\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the location where you wish to delete an element\n");
scanf("%d",&x);
if(x>n)
printf("Invalid Input\n");
else
{
for(i=x-1;i<n;i++)
{
a[i]=a[i+1];
}
n=n-1;
printf("Array after deletion is\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
}
}
return 0;
}
Happy Learning – If you require any further information, feel free to contact me.