ALGORITHM
- Ask for the element to be removed.
- Perform a search algorithm (binary or linear) to check the validity of the inserted element.
- If the element exist then perform the 4th step or else go to step 5.
- Shift all the elements from index + 1 to it’s left position i.e. index - 1. this will reduce the array size and overlap with the existing element in the position.
- Otherwise print “element not found”
- exit
SOURCE CODE
#include <stdio.h>
#include <conio.h>
int main ()
{
int arr[50];
int pos, i, num;
printf (" \\n Enter the number of elements in an array: \\n ");
scanf (" %d", &num);
printf (" \\n Enter %d elements in array: \\n ", num);
for (i = 0; i < num; i++ ){
printf ("arr[%d] = ", i);
scanf ("%d", &arr[i]);
}
printf( " Define the position of the array element where you want to delete: \\n ");
scanf (" %d", &pos);
if (pos >= num+1){
printf (" \\n Deletion is not possible in the array.");
}
else{
for (i = pos - 1; i < num -1; i++)
{
arr[i] = arr[i+1];
}
printf (" \\n The resultant array is: \\n");
for (i = 0; i< num - 1; i++)
{
printf (" arr[%d] = ", i);
printf (" %d \\n", arr[i]);
}
}
return 0;
}