C Program to delete an element from the array

Array

In this Post we’re going to learn how to delete an element from the array in 2 conditions

  1. Position of the element to be deleted is given.
  2. Elements to be deleted is given.

Position of the element to be deleted is given.

This C program is to delete an element from an array from a specified location/ position. For example, if an array a consists of elements a={7,8,12,3,9} and if we want to delete element at position 3 then the new array would be a={7,8,12,9} (as array starts from index 0).

Logic
  • Take input array ‘a’ and no of elements(n) as 5
  • Let us take elements for array a={7,8,12,3,9}.
  • Let us delete an element from the array ,which is at position 3.
  • 1st iteration  for(i=pos;i<n-1;i++)  i.e.  for(i=3;i<5-1;i++)  i.e.  for(i=3;=3<4;i++)
  • a[i]=a[i+1] i.e. a[3]=a[3+1] i.e. a[3]=a[4] i.e. a[3]=9
  • Now we move out of the for loop as i will be 4 which is not less than n-1(4).
  • n=n-1 i.e. n=5-1 i.e n=4 //We are decreasing the no of elements

Hence our new array would be a={7,8,12,9}.

Below is the Implementation of above logic
#include<stdio.h>
 
void main() 
{
    int a[100],i,n,pos;
 
    printf("\nEnter no of elements\n");    
    scanf("%d",&n);
    
    printf("Enter the elements\n");
    for (i=0;i<n;i++) 
    {
        scanf("%d",&a[i]);
    }
    
    printf("Elements of array are\n");
    for(i=0;i<n;i++) 
    {
        printf("a[%d] = %d\n",i,a[i]);
    }

    printf("Enter the position from which the number has to be deleted\n");
    scanf("%d",&pos);
    for(i=pos;i<n-1;i++)
    {
        a[i]=a[i+1];
    }
    n=n-1;
    printf("\nOn Deletion, new array we get is\n");
    for(i=0;i<n;i++) 
    {
        printf("a[%d] = %d\n",i,a[i]);
    }
}
Output
Enter no of elements
Enter the elements
Elements of array are
a[0] = 7
a[1] = 8
a[2] = 12
a[3] = 3
a[4] = 9
Enter the position from which the number has to be deleted

On Deletion, new array we get is
a[0] = 7
a[1] = 8
a[2] = 12
a[3] = 9

Element to be deleted is given

This program is just an extension of the previous program. Extra work to be done here is that first we have to check if it exists inside the array or not. If it exists then we will apply the previous logic and if it doesn’t exist in array then we will return a message saying the element to be deleted was not found. So let’s write the program.]

#include<stdio.h>
 
int main() 
{
    int a[100],i,n,pos;
 
    printf("\nEnter no of elements\n");    
    scanf("%d",&n);
    
    printf("Enter the elements\n");
    for (i=0;i<n;i++) 
    {
        scanf("%d",&a[i]);
    }
    
    printf("Elements of array are\n");
    for(i=0;i<n;i++) 
    {
        printf("a[%d] = %d\n",i,a[i]);
    }

    printf("Enter the the number has to be deleted\n");
    int p;
    scanf("%d",&p);
    int found=0;
    pos=0;
    for(i=0; i<n; i++)
    {
        /* 
         * If element is found in array then raise found flag
         * and terminate from loop.
         */
        if(a[i] == p)
        {
            found = 1;
            pos=i;
            break;
        }
    }
    
    if(found==1)
    {
    	for(i=pos;i<n-1;i++)
    {
        a[i]=a[i+1];
    }
    n=n-1;
    printf("\nOn Deletion, new array we get is\n");
    for(i=0;i<n;i++) 
    {
        printf("a[%d] = %d\n",i,a[i]);
    }
    	
    }
else 
printf("Element is not present in array");
    
}
Output
Enter no of elements
Enter the elements
Elements of array are
a[0] = 7
a[1] = 8
a[2] = 12
a[3] = 3
a[4] = 9
Enter the position from which the number has to be deleted

On Deletion, new array we get is
a[0] = 7
a[1] = 8
a[2] = 12
a[3] = 9
Important Notice for college students

If you’re a college student and have skills in programming languages, Want to earn through blogging? Mail us at geekycomail@gmail.com

For more Programming related blogs Visit Us Geekycodes . Follow us on Instagram.

Leave a Reply

%d bloggers like this: