C Program to Delete an Element in an Array

How do you write a C Program to Delete an Element in an Array using a for loop and if else statement with an example.?

C Program to Delete an Element in an Array using If

This C program to delete elements allows users to enter the One Dimensional Array’s size and row elements. Next, we are using For Loop to find and delete an element from it at specified index positions.

#include <stdio.h>
 
int main()
{
	int Array[10], Position, i, Size;
	
	printf("\n Please Enter Number of elements in an array  :   ");
	scanf("%d", &Size);
	
	printf("\n Please Enter %d elements of an Array \n", Size);
	for (i = 0; i < Size; i++)
	{
    	scanf("%d", &Array[i]);
   	}     
 
  	printf("\n Please Enter a Valid Index Position of a Element that you want to Delete  :  ");
  	scanf("%d", &Position);
  	
	if(Position < 0 || Position >= Size)
  	{
  		printf("\n Please Enter a Valid Index Position between 0 and %d", Size-1);
  	}
  	else
  	{
  		for (i = Position; i < Size; i++)
   		{
	    	Array[i] = Array[i + 1];
   		}
   		Size--;
	}
 	printf("\n Final Array after Deleteing an Array Elemnt is:\n");
 	for (i = 0; i < Size; i++)
  	{
 		printf("%d\t", Array[i]);
  	}	     
 	return 0;
}
C Program to Delete an Element in an Array 1

This Program asks the User to enter the Index position. a[10] means the Index position starts at 0 and ends at 9.

First, we used the If statement to check whether the user-specified index position is less than 0 or greater than or equal to size. If it is true, then it will print an error message. Otherwise, it will enter into the Else Block.

Within the Else block, we used For Loop. It will make sure that the position values are between the Position and the maximum size value of the One Dimensional Array.

User Entered Values for C Program to Delete an Element in an Array example: Size = 4, Elements = 25 69 73 224, and Position = 2

Condition inside the If statement is False. So, the C Programming compiler will enter into the Else block

For Loop – First Iteration: for(i = 2; 2 < 4; 2++)
The condition is True
Array[i] = Array[i + 1]
[2] = [2 + 1]
It means 224

Second Iteration: for(i = 3; 3 < 4; 3++)
The condition is True
[3] = [3 + 1] // There is nothing at this position

Next, we used Size– to adjust the Array size. It means the Size will become 3, and the final one will be 25 69 224. let me try to delete the non-existing item


 Please Enter Number of elements in an array  :   4

 Please Enter 4 elements of an Array 
25 46 78 96

 Please Enter a Valid Index Position of a Element that you want to Delete  :  4

 Please Enter a Valid Index Position between 0 and 3
 Final Array after Deleteing an Array Elemnt is:
25	46	78	96	

3rd OUTPUT


 Please Enter Number of elements in an array  :   5

 Please Enter 5 elements of an Array 
23 56 89 71 256

 Please Enter a Valid Index Position of a Element that you want to Delete  :  0

 Final Array after Deleteing an Array Elemnt is:
56	89	71	256	

C Program to Delete an Array Element using for loop

This program is the same as above. However, this time, we ask the user to specify the location (not the actual index position). It means if the user enters 4, then this program will remove the value at index position 3.

#include <stdio.h>
 
int main()
{
	int Array[10], Position, i, Size;
	
	printf("\n Please Enter Number of elements  :   ");
	scanf("%d", &Size);
	
	printf("\n Please Enter %d elements \n", Size);
	for (i = 0; i < Size; i++)
	{
    	scanf("%d", &Array[i]);
   	}     
 
  	printf("\n Please Enter the location of a Element that you want to Delete  :  ");
  	scanf("%d", &Position);
  	
	if(Position <= 0 || Position > Size)
  	{
  		printf("\n Please Enter a Valid Position between 1 and %d", Size);
  	}
  	else
  	{
  		for (i = Position-1; i < Size; i++)
   		{
	    	Array[i] = Array[i + 1];
   		}
   		Size--;
	}
 	printf("\n Final one after Deleting is:\n");
 	for (i = 0; i < Size; i++)
  	{
 		printf("%d\t", Array[i]);
  	}	     
 	return 0;
}
C Program to Delete an Element in an Array using for loop

2nd OUTPUT


 Please Enter Number of elements  :   4

 Please Enter 4 elements
99 75 35 55

 Please Enter the location of a Element that you want to Delete  :  4

 Final one after Deleting is:
99	75	35	

OUTPUT 3

 Please Enter Number of elements  :   5

 Please Enter 5 elements
25 698 10 55 9

 Please Enter the location of a Element that you want to Delete  :  0

 Please Enter a Valid Position between 1 and 5
 Final one after Deleting is:
25	698	10	55	9