In this tutorial, we will write a C program to delete elements from an array at a specified position. We will specifically learn two different programs:
- Delete an element by specifying the position.
- Delete an element by specifying the value.
Example: By position.
Input
Input array elements: 11 22 30 42 50
Specify the position to delete: 3
Output
Array elements: 11 22 42 50
Example: By Value.
Input
Input array elements: 11 22 30 42 50
Specify the value to delete: 22
Output
Array elements: 11 30 42 50
Before that, you should have knowledge on the following C topics:
C program to delete an element from an array at specified position
Explanation: The position of an element to be removed is entered. Then using loop traverse to that position. Once you get to the specified position, shift all the elements from index + 1 by 1 position to the left.
And if the position is not present that is the number of elements present in an array is 5 but the user entered the 6th position to delete, in that case, the program will display the message “Position Not Valid”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | #include <stdio.h> int main() { int arr[50], pos, i, size; printf("Enter the size of an array: "); scanf("%d", &size); printf("Enter %d elements in an array:\n", size); for (i = 0; i < size; i++) scanf("%d", &arr[i]); printf("Array before deletion of element:\n"); for (i = 0; i < size; i++) printf("%d ", arr[i]); printf("\n\nEnter the location of the element to delete: "); scanf("%d", &pos); if (pos >= size + 1) printf("Entered Position Not Valid"); else for (i = pos - 1; i < size - 1; i++) arr[i] = arr[i + 1]; //Location shifted printf("\nArray after deletion of element:\n"); for (i = 0; i < size - 1; i++) printf("%d ", arr[i]); return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | Enter the size of an array: 5 Enter 5 elements in an array: 10 20 30 40 50 Array before deletion of element: 10 20 30 40 50 Enter the location of the element to delete: 2 Array after deletion of element: 10 30 40 50 |
C Program to delete Element in an Array by specifying the Value
Explanation: Here instead of a position of an element, the value of the element is specified to delete. Similarly, the program traverse until the element is found and then shifts all the elements from index + 1 by 1 position to the left.
And if the value is not that is the entered value does not match with any element present in an array, in that case, the program will display the message “Element Not Valid”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | #include <stdio.h> int main() { int arr[50], element, i, size, pos; int element_found = 0; printf("Enter the size of an array: "); scanf("%d", &size); printf("Enter %d elements\n", size); for (i = 0; i < size; i++) scanf("%d", &arr[i]); printf("\nArray before Deletion:\n"); for (i = 0; i < size; i++) printf("%d ", arr[i]); printf("\n\nEnter the element to be deleted: "); scanf("%d", &element); // check the element to be deleted is in array or not for (i = 0; i < size; i++) { if (arr[i] == element) { element_found = 1; pos = i; break; // terminate the loop } } if (element_found == 1) // for element exists { for (i = pos; i < size - 1; i++) arr[i] = arr[i + 1]; } else //for element does not exists printf("\nElement %d is not present in an Array. Try Again!", element); printf("\nArray after Deletion:\n"); for (i = 0; i < size - 1; i++) printf("%d ", arr[i]); return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | Enter the size of an array: 5 Enter 5 elements 10 20 30 40 50 Array before Deletion: 10 20 30 40 50 Enter the element to be deleted: 30 Array after Deletion: 10 20 40 50 |