Deletion Of Element In Array
Arrays

Deletion Of Element In Array In C

In this program, we will discuss how to write a C program Deletion Of Element In Array. So, let’s start writing the program to delete the element from an array in the below article.

Deletion Of Element In Array In C:

In this section, we will try to understand according to which knowledge this type of program will run. First of all, you have to know about the array. What is an array? The answer is the array is a non-primitive data type that consists of more than one value of a single variable.

We are not using any function in the program or any complicated thing to write this program. We are simply using loops(for loop), arrays, and some variables. In our program, we assign the value of size to 5. You can also take the values from the user to better understand the program and its logic.

We simply declare the integer type array ‘i’ variable is used to start the loop and ‘the pos’ variable is for the position because we have to enter the position in the program later to delete the number from our entered position. Now we have to take the elements from the user by using for loop.

The for loop is starting with 0 because the indexing in C language starts with zero. This is why we start the loop from 0 and. The loop will run from 0 to less than size because there is a little bit of difference. Suppose we have given the size 5 but C is an indexing language so it will start from zero it will execute 5 times but from,0 to 4 not 5. That is a little bit confusing for some students hope so you will get the point.

After taking the elements of the array from the user now whether that compiler will from which position do you want to delete the number. So, we simply take the value of position from the user, and in the next step, we will give the condition at if the entered position is greater than size it will print that deletion is not possible.

In the else statement we will give the whole logic to delete the element of the array if we enter position= 3. The loop will start from 2 and check for the condition as we know 2 is less than 5 or size-1. So, it will move to the body of the loop and assigns the value a[i+1] to a[i]. This is how our program works. And in the last statement, it will print out the result as we want.

Deletion Of Element In Array Program:

#include<stdio.h>
#define size 5
int main(){
int a[size],i,pos;
printf("Enter %d Elements Of Array\n",size);
for(i=0;i<size;i++){
scanf("%d",&a[i]);
}
printf("Enter The Position To Delete The Element :\n");
scanf("%d",&pos);
if(pos>=size+1){
printf("DeletioN Not Possible\n");
}
//for better understanding write elements of arrays in comments
//elements are {1,2,3,4,5}
else{
for(i=pos-1;i<size-1;i++){
a[i]=a[i+1];
}
for(i=0;i<size-1;i++){
printf("%d,",a[i]);
}
}
}

Output:

Deletion Of Element In Array
Deletion Of Element In Array

 

 

 

 

 

Conclusion:

From the above program we will conclude that how to write a c program of Deletion Of Element In Array and we will show you the output according to the above program. So, this is all about today’s article. We hope that you like our program. If you are searching for more programs of an array then click on the link to direct to the arrays page Arrays.

 

Leave a Reply

Your email address will not be published. Required fields are marked *