Skip to main content
Deletion in array
Deletion an element from the array means removing a data element from an existing array.If the element has to be deleted from the end of the existing array,We have to subtract 1 from the upper_bound.
Write a program to delete a number from a given location in an array
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,p,arr[10];
clrscr();
printf("\n Enter the size of the array");
scanf("%d",&n);
printf("\n Enter the element of the array:");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
printf("\n Enter the position from which the number has to be deleted:");
scanf("%d",p);
}
for(i=p;i<n;i++)
{
arr[i]=arr[i+1];
n--;
printf("\n the array after deletion is:");
}
for(i=0;i<n;i++)
{
printf("\n arr[%d]=%d",i,arr[i]);
getch();
}
}
Output
Enter the size of the array:5
Enter the element of the array:
1 2 3 4 5
Enter the position from which the number has to be deleted:3
The array after deletion is:
arr[0]=1
arr[1]=2
arr[2]=3
arr[3]=4
arr[4]=5
Comments
Post a Comment