Type Here to Get Search Results !

Web Stories

C Program to Delete an Element from an Array with Explanation

In this post I'm gonna share you amazing unique C Program to Delete an Element from an Array. In order to clear all Basics of C Programming you can visit my Full C Programming Course for Free.

Delete an Element from an Array

www.dheerajpatidar.com

Deleting an element does not affect the actual size of array. The size of the array remain same as we declare.

C Program to Delete an Element of an Array with user Input:

#include <stdio.h>

int main(){

    int arr[20];

    int arrsize, item, loc;

    printf("How Many Elements: "); 

    scanf("%d",&arrsize);

 

    for(int i=0; i<arrsize; i++){

        printf("Enter Element %d: ",i+1);

        scanf("%d",&arr[i]);

    }

    printf("\n");

 

    printf("Enter which Location Element You want to Delete: ");

    scanf("%d",&loc);

 

    item=arr[loc-1];

 

    for(int i=loc-1; i<arrsize-1; i++){

        arr[i]=arr[i+1];

    }

    arrsize--;

 

    printf("At Location %d element %d is Deleted\n\n", loc, item);

 

    printf("Now After Deletion Array Elements:\n");

    for(int i=0; i<arrsize; i++){

        printf("Element %d: %d\n",i+1, arr[i]);

    }

    return 0;

}


Explanation of the program:

The above program is about delete an element from an array, where I have taken an array of size 20. Program will take values from the end-user.

In order to delete an element, we have taken following steps:

1) First of all we have declare an array of size 20.

int arr[20];


2) After that, we have asked user "How may Elements" he/she wants to Enter, and according to the user's choice we have taken each of the element using for loop.

 printf("How Many Elements: "); 

 scanf("%d",&arrsize);

 

 for(int i=0; i<arrsize; i++){

     printf("Enter Element %d: ",i+1);

     scanf("%d",&arr[i]);

 }

 printf("\n");


3) After taking all the elements from the user, the program will ask to  that "Enter which Location Element You want to Delete: ".

  printf("Enter which Location Element You want to Delete: ");

scanf("%d",&loc);


4) After taking the desired location, program will store the respective position of element in an auxiliary variable "item", in order to use it in future for just printing.

  item=arr[loc-1];


5) After that, using for loop we have shift the elements from the location entered by the user (loc-1) to last element, in backward direction. For example, if user wanna delete 4th element then the program will shift 5th, 6th, 7th,....,nth elements in backward direction.

 for(int i=loc-1; i<arrsize-1; i++){

       arr[i]=arr[i+1];

 }


6) After the shifting is done, we have to reduce the size of the array by 1. and have to the message to the console about success.

  arrsize--;

printf("At Location %d element %d is Deleted\n\n", loc, item);


7) The last step of the program is to print all the elements of the array for confirmation (It is optional).

 printf("Now After Deletion Array Elements:\n");

 for(int i=0; i<arrsize; i++){

    printf("Element %d: %d\n",i+1, arr[i]);

 }


Conclusion

In this post we have discussed "C Program to Delete an Element from an Array with Explanation".

You can visit our C Tutorials Series, where I have discussed each of the concept of C Programming in detail.

I hope you enjoy this post, and have got useful content here.

Thanks for Visit!

Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.