Deleting an element from an array involves, just placing the next number to the position of the deleting element. For example: if the array elements are 10,5,9,4 and we want to delete element '5', then the array becomes 10,9,4. We place the '9' to the position of element '5', and 4 to 9.
In this c program, we need to make an array of elements, and we need to print that array. We need to delete one number from the list or array, which is given by the user. In this c program, what we are doing is to declare an array and accepts the input from the user. We add the elements into the array and display the array.
Now we ask the user to enter the element which is to delete from the array. After we got the number in a variable, we open a for loop from zero to n. Inside the 'for loop
', we check each element for the user input; if the number is found, we make a variable 'found' equal to one. Now what we are doing is to make the position of the element to get deleted is changed to i+1 so the next number will occupy the space of the last digit; hence we get to delete the number user wants to delete.
STEP 1: Include the Header files to use the built-in functions in the C program.
STEP 2: Declare the integer Array nvectx[10] and the integer variable i, n, pos, element, found and set found=0.
STEP 3: Read the number of elements into n.
STEP 4: Read the numbers from the user and save them into vectx[i] using for loop
.
STEP 5: Display the input array as vectx[i] using for loop
.
STEP 6: Read the element which wants to delete from the user into the variable component.
STEP 7: By using a for loop
with the condition i
STEP 8: Check if vectx[i]==element then set found=1,pos=i and break.
STEP 9: Repeat step 7.
STEP 10: If found==1 then do step 11.else do step 15.
STEP 11: Set i=pos.
STEP 12: Check i
STEP 13: Increment 'i' by 1 and do step 12.
STEP 14: Display the resultant vector is, the elements of vectx[i] using for loop
.
STEP 15: Display the "element that is not found" in the vector.
#include <stdio.h>
void main() {
int vectx[10];
int i, n, pos, element, found = 0;
printf("Enter how many elements\n");
scanf("%d", & n);
printf("Enter the elements\n");
for (i = 0; i < n; i++) {
scanf("%d", & vectx[i]);
}
printf("Input array elements are\n");
for (i = 0; i < n; i++) {
printf("%d\n", vectx[i]);
}
printf("Enter the element to be deleted\n");
scanf("%d", & element);
for (i = 0; i < n; i++) {
if (vectx[i] == element) {
found = 1;
pos = i;
break;
}
}
if (found == 1) {
for (i = pos; i < n - 1; i++) {
vectx[i] = vectx[i + 1];
}
printf("The resultant vector is \n");
for (i = 0; i < n - 1; i++) {
printf("%d\n", vectx[i]);
}
} else
printf("Element %d is not found in the vector\n", element);
} /* End of main() */
Run 1 Enter how many elements 5 Enter the elements 30 10 50 20 40 Input array elements are 30 10 50 20 40 Enter the element to be deleted 35 Element 35 is not found in the vector Run 2 Enter how many elements 4 Enter the elements 23 10 55 81 Input array elements are 23 10 55 81 Enter the element to be deleted 55 The resultant vector is 23 10 81