C Program to Delete Integer from Array
C program to delete an integer from array: Algorithm to delete an element from an integer array would be, first accept array elements then accept the element to be deleted then search the element in the array; if the element is found then adjust the array such that the elements next to the element being deleted are shifted to left by position. This way the element will be removed from the array.
c program to delete an element from an array
/* C program to accept an array of integers and delete the specified integer from the list */
#include<stdio.h>
int main()
{
int array[10];
int i, n, pos, element, found = 0;
printf("\n Enter how many elements:- ");
scanf(" %d", &n);
printf("\n Enter the elements:- ");
for(i=0;i<n;i++)
{
scanf(" %d", &array[i]);
}
printf("\n The array elements are:- ");
for (i=0;i<n;i++)
{
printf("%d\n", array[i]);
}
printf("\n Enter the element to be deleted:- ");
scanf(" %d", &element);
for(i=0;i<n;i++)
{
if (array[i]==element)
{
found = 1;
pos = i;
break;
}
}
if(found==1)
{
for(i=pos;i<n-1;i++)
{
array[i] = array[i + 1];
}
printf("The resultant vector is \n");
for(i=0;i<n-1;i++)
{
printf("%d\n", array[i]);
}
}
else
printf("Element %d is not found in the vector\n", element);
return 0;
}
/* Output of above code / Runtime Cases:-
Enter how many elements:- 4
Enter the elements:-
145
234
678
887
The array elements are:-
145
234
678
887
Enter the element to be deleted:- 234
The resultant vector is
145
678
887
*/
Get This Program: