Deleting an Element From Array Using C

CCBeginner
Practice Now

Introduction

An array is a collection of similar data items stored at contiguous memory locations and consists of elements. An element can be accessed by indexing the position of the corresponding memory location. Sometimes it becomes essential to delete any of the array element based on its position or value. In this lab, we will learn how to remove an element from the array based on its position and value.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/ControlFlowGroup(["`Control Flow`"]) c(("`C`")) -.-> c/PointersandMemoryGroup(["`Pointers and Memory`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/BasicsGroup -.-> c/operators("`Operators`") c/ControlFlowGroup -.-> c/if_else("`If...Else`") c/ControlFlowGroup -.-> c/for_loop("`For Loop`") c/ControlFlowGroup -.-> c/break_continue("`Break/Continue`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/PointersandMemoryGroup -.-> c/memory_address("`Memory Address`") c/FunctionsGroup -.-> c/function_parameters("`Function Parameters`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") subgraph Lab Skills c/output -.-> lab-123230{{"`Deleting an Element From Array Using C`"}} c/variables -.-> lab-123230{{"`Deleting an Element From Array Using C`"}} c/data_types -.-> lab-123230{{"`Deleting an Element From Array Using C`"}} c/operators -.-> lab-123230{{"`Deleting an Element From Array Using C`"}} c/if_else -.-> lab-123230{{"`Deleting an Element From Array Using C`"}} c/for_loop -.-> lab-123230{{"`Deleting an Element From Array Using C`"}} c/break_continue -.-> lab-123230{{"`Deleting an Element From Array Using C`"}} c/user_input -.-> lab-123230{{"`Deleting an Element From Array Using C`"}} c/memory_address -.-> lab-123230{{"`Deleting an Element From Array Using C`"}} c/function_parameters -.-> lab-123230{{"`Deleting an Element From Array Using C`"}} c/function_declaration -.-> lab-123230{{"`Deleting an Element From Array Using C`"}} end

Create a C project

First, we need to create a C project in the code editor. Create a new file "main.c" under the ~/project/ directory, and write the following code:

#include <stdio.h>

int main()
{
  return 0;
}

Delete an element from Array based on position

In this step, we will write a program in C language that accepts an array of integers, the size of the array, and a position of an element to delete. Then, we will delete the element and print the updated array.

#include <stdio.h>

int main()
{
    int arr[100], position, c, n;

    printf("Enter the number of elements in array: ");
    scanf("%d", &n);

    printf("Enter %d elements:\n", n);
    for(c = 0; c < n; c++)
        scanf("%d", &arr[c]);

    printf("Enter the location of the element to delete: ");
    scanf("%d", &position);

    if (position >= n + 1)
        printf("Deletion not possible.\n");
    else
        for (c = position - 1; c < n - 1; c++)
            arr[c] = arr[c+1];

    printf("The updated array is: ");
    for(c = 0; c < n-1; c++)
        printf("%d ", arr[c]);

    return 0;
}

Code Explanation:

  • We create an integer array arr[100], which can store a maximum of 100 elements.
  • We take input integer n from the user, which represents the number of elements in the array.
  • The for loop is used to accept input from the user using the scanf function into the array arr.
  • We take the position of the element to be deleted as input from the user.
  • We check if the user input position is valid or not; it should be between 1 and n.
  • If the position is valid, we place the element left to the position to the place of the deleted element, which is achieved by implementing a loop for moving the element to its left places.
  • Finally, we output the updated array using the printf statement.

Delete an Element from Array Based on Value

In this step, we will write a program in C to delete an array element based on the value entered by the user.

#include<stdio.h>

int main()
{
    int arr[10], element, c, n, pos, found=0;

    printf("Enter the number of elements in array: ");
    scanf("%d", &n);

    printf("Enter %d elements:\n", n);
    for (c = 0; c < n; c++)
        scanf("%d", &arr[c]);

    printf("Enter the element to delete: ");
    scanf("%d", &element);

    for (c = 0; c < n; c++)
    {
        if (arr[c] == element)
        {
            found = 1;
            pos = c;
            break;
        }
    }

    if (found == 1)
    {
        for (c = pos; c < n - 1; c++)
            arr[c] = arr[c+1];

        printf("The updated array is: ");
        for (c = 0; c < n - 1; c++)
            printf("%d ", arr[c]);
    }

    else
        printf("Element not found in array.");

    return 0;
}

Code Explanation:

  • We create an integer array arr[10].
  • We store the number of elements to be added from the user input variable n.
  • We use a for loop for taking n integers one by one by scanf function into integer array arr.
  • We take the input of the element which we want to delete from the user.
  • We traverse the array by using a for loop from 0 to n-1 and then using an if statement we check if the element entered by the user is equal to any of the elements of the array or not.
  • If it is equal, then we declare a variable pos in which we store the index of that element.
  • We use another for loop to delete the element and shift the remaining elements of the array.
  • Finally, we output the updated array using the printf statement.

Summary

In this lab, we have learned how to delete an element from an array by its position or value in C programming. The program to delete an element from an array takes the array, its size, and the position/value of the element to be deleted as inputs, a for loop is used to traverse the array, then the position or value of the targeted element is found and then a loop is used to delete the element. This is achieved by copying the element present on the right of the element to be deleted to the left till the end of the array. After deletion, the array size is reduced by one.

Other C Tutorials you may like