11.10.25

Array Element Deletion in C

 



Understanding Array Element Deletion in C 

Hey everyone! 
Today, I’m going to walk you through a simple yet interesting C program that demonstrates how to delete elements from an array one by one while showing the line numbers and the current state of the array at every step.


Code

#include <stdio.h>

int main() {
    int arr[3] = {10, 20, 30};
    int size = 3;

    printf("\nLine Number: %d\tInitial Array:", 8);
    for (int element_index = 0; element_index < size; element_index++) {
        printf("\nLine Number: %d\tElement_index: %d\tValue: %d", 9+element_index, element_index, arr[element_index]);
    }

    printf("\nLine Number: %d\tDeleting last element", 13);
    size--;
    printf("\nLine Number: %d\tArray now has %d elements", 14, size);
    for (int element_index = 0; element_index < size; element_index++) {
        printf("\nLine Number: %d\tElement_index: %d\tValue: %d", 15+element_index, element_index, arr[element_index]);
    }

    printf("\nLine Number: %d\tDeleting last element", 19);
    size--;
    printf("\nLine Number: %d\tArray now has %d elements", 20, size);
    for (int element_index = 0; element_index < size; element_index++) {
        printf("\nLine Number: %d\tElement_index: %d\tValue: %d", 21+element_index, element_index, arr[element_index]);
    }

    printf("\nLine Number: %d\tDeleting last element", 24);
    size--;
    printf("\nLine Number: %d\tArray now has %d elements", 25, size);

    printf("\nLine Number: %d\tProgram Ended.", 28);

    return 0;
}

Step-by-Step 

Step 1: Initialize the Array

int arr[3] = {10, 20, 30};
int size = 3;

We start with a small array of 3 elements: 10, 20, and 30.
The variable size keeps track of how many elements the array currently has.

Step 2: Display Initial Array

The program prints all elements with their line numbers and index positions:

Line Number: 8   Initial Array:
Line Number: 9   Element_index: 0   Value: 10
Line Number: 10  Element_index: 1   Value: 20
Line Number: 11  Element_index: 2   Value: 30

This helps us visualize the array before we start deleting anything.

Step 3: Deleting the Last Element

In each deletion block:

  1. We reduce the size by one (size--).

  2. We print the updated size.

  3. We display the remaining elements.

For example, after the first deletion:

Line Number: 13  Deleting last element
Line Number: 14  Array now has 2 elements
Line Number: 15  Element_index: 0   Value: 10
Line Number: 16  Element_index: 1   Value: 20

The last element 30 is no longer considered part of the array.

Step 4: Repeat Deletion

The same process repeats until all elements are deleted:

  • Second deletion removes 20

  • Third deletion removes 10

Finally, size becomes 0, meaning the array is now empty!

Step 5: End of Program

The program finishes with:

Line Number: 28  Program Ended.

This marks the completion of the array deletion process.

Key Points

  • Arrays have a fixed size in C, so we can’t “delete” elements physically instead, we reduce the logical size.

  • The elements remain in memory, but we ignore the deleted parts by decreasing the size.

  • Using line numbers helps in debugging and understanding program flow.

Why ?

  • It demonstrates array operations clearly.

  • It helps beginners understand how deletion works conceptually.

  • The line numbers and print statements make it easy to trace every step of execution.

Output

Line Number: 8	Initial Array:
Line Number: 9	Element_index: 0	Value: 10
Line Number: 10	Element_index: 1	Value: 20
Line Number: 11	Element_index: 2	Value: 30
Line Number: 13	Deleting last element
Line Number: 14	Array now has 2 elements
Line Number: 15	Element_index: 0	Value: 10
Line Number: 16	Element_index: 1	Value: 20
Line Number: 19	Deleting last element
Line Number: 20	Array now has 1 elements
Line Number: 21	Element_index: 0	Value: 10
Line Number: 24	Deleting last element
Line Number: 25	Array now has 0 elements
Line Number: 28	Program Ended.


No comments:

Post a Comment

rating System

Loading...

A Friendship Story of Learning Data Structures with C

Sr. No. DSU BLOGS CHAPTERS 1 Array Operations in C, The Group of Friendship (Create, Insert, Delete ...