Delete Element from Array in C Language

program-to-delete-an-element-from-array-in-c

Program Description:

In our earlier article, We have looked at the program to Insert an element in an array and In today’s article, we will write a Program to Delete Element from Array in C Langauge. The program accepts an array and element position from the user and deletes the element at the given position.

Example Input and Output:

Input:

Enter desired array size(1-1000): 10

Please enter array elements(10) : 34 82 98 23 45 67 12 34 56 76

Enter the position(1-10) of element to be deleted : 5

Output:

Original Array: 34 82 98 23 45 67 12 34 56 76

Array after deleting element: 34 82 98 23 67 12 34 56 76

If you observe, The element at the position 5 ( position starts from 1, so position = index + 1) which is 45 is removed from the resultant array.

Prerequisites:

It is recommended to know the basics of the C Arrays and Functions. Please read the following articles to learn more about them.

Algorithm to Delete Element from Array in C:

Take the array values( numbers) and the element position to be deleted from the user.

Let’s say the array name is numbers array. And the element position to be deleted is position, and the Array size is size.

To Delete the element at the position from the numbers array of size size.

  • Shift all the elements in the numbers array that has an index greater than ( position-1) to the left by one position.
  • This will overwrite the element at the index ( position-1) and removes it from the array.
  • Finally, Reduce the array size by 1. decrement size variable – size;

This way we can Remove or Delete an element from the array in the C programming language.

Delete-an-element-from-array-algorithm

📢This program is part of the Array Practice Programs in C language.

Delete Element from Array in C Program Explanation:

  1. Declare the numbers array with 1000 elements, Also declare other variables like i, size, iNum, and position.
  2. Take the desired array size from the user and store it in size variable.
  3. Define a read() function to update the array elements with user-provided values.
    • Prompt the user to provide the array elements and update the array.
  4. Call the read() function from the main() function and pass the numbers array and its size as the actual arguments. The read() function updates the numbers array with user values.
  5. Take the position of the element to be deleted from the user. The position is not equal to index, ( index = position-1)
  6. Define another function called display() to print the array elements on the standard output.
    • Use a For Loop to traverse the array and print the elements
  7. To delete element at the position. Shift all elements greater than the position to left by one position.
    • Use For loop to traverse from the position-1 till the size-1for(i=position-1; i<size; i++)
    • Replace the element at the i with i+1. – numbers[i] = numbers[i+1];
  8. Repeat the above step-7 till we reach the end of the array.
  9. As we deleted an element, Reduce the array size by 1 – Decrement the size variable – size--;
  10. Print the array after deleting the element at position on the console by calling the display() function.

Program to Delete Element from Array in C:

Here is the program to delete an element from an array in c programming language.

Program Output:

Let’s compile and run the program

$ gcc delete-elem-arr.c

Run the program.

Test Case 1: Normal Cases:

delete-an-element-from-array-in-c-program-output

As we can see from the above output, The program is deleted the element from the array.

For example, If the input array is 1 2 3 4 5 and we want to delete the element at the position 2, Then the resultant array is 1 3 4 5. ( index = position -1)

Test Case 2: Boundary Cases:

Test Case 3: Negative Cases:

If the user enters the wrong position, Then the program displays an error message saying the Invalid Position.

Delete Element from Array using a user-defined function in C Language:

In the above program, We used two functions read() and display() to read and print the array elements. Let’s create another function named deleteAt to delete an element from the array.

The deleteAt the function should take an input array, the position, Then deletes the element at the position and provides the resultant array back to the caller.

Here is the rewritten version of the above program, Where we defined the deleteAt function.

📢 As the deleteAt function is defined after calling it from the main() function, We need to declare the function at the start of the program ( Forward declaration of the function).

  • int deleteAt(int numbers[], int size, int position);

Delete an Array Element Program Output:

Compile and Run the program using any compiler. We are using the GCC Compiler on ubuntu Linux system.

Test Case 1: Positive Cases:

delete-an-element-from-array-using-deleteAt-function

Test case 2: Negative Cases:

As you can see from the above output, The program is generating the desired results.

Related Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

2 Responses

  1. […] Delete an element from an Array […]

  2. […] C Program to Delete an Element in Array […]

Leave a Reply