C Program to insert an Element in an Array

How to write a C Program to insert an element in an Array at a user-specified location using For Loop and While Loop.?

C Program to insert an Element in an Array using For Loop

This C program allows the user to enter the array size, Elements of an Array, Location of an element, and the value of an element. Using C For Loop, we are going to insert the value or element in an Array at the user-specified location.

#include <stdio.h>
 
int main()
{
  int Array[10], Position, i, Number, Value;
 
  printf("\nPlease Enter Number of elements in an array\n");
  scanf("%d", &Number);
 
  printf("\nPlease Enter %d elements of an Array \n", Number);
  for (i = 0; i < Number; i++)
   {
     scanf("%d", &Array[i]);
   }     
 
  printf("\nPlease Enter the location of a Element you want to insert\n");
  scanf("%d", &Position);
 
  printf("\nPlease Enter the value of an Array Emenent to insert\n");
  scanf("%d", &Value);
 
  for (i = Number - 1; i >= Position - 1; i--)
   {
	     Array[i+1] = Array[i];
   }
  Array[Position-1] = Value;
  
 printf("\n Final Array after Inserting an  Elemnt is:\n");
 for (i = 0; i <= Number; i++)
  {
 	printf("%d\t", Array[i]);
  }     
 
 return 0;
}
C program to insert an element in an array using For Loop

In this c program to insert an element in an array example, the below for loop iterate each cell present in the Array[3] array. The condition inside the for loops (i < Number) ensures that the compiler will not exceed the array size. Otherwise, the array will overflow. please refer to Arrays in C article to understand the concept of Array size, index position, etc.

The scanf statement inside this will store the user entered values as an array element such as a[0], a[1], a[2]

for (i = 0; i < Number; i++)
 {
   scanf("%d", &Array[i]);
 }     

The below C programming printf statement asks the user to enter the location to insert the new element. And the scanf statement will assign the user entered value to the Position Variable

printf("\nPlease Enter the location of a Element you want to insert\n");
scanf("%d", &Position);

The Following printf statement allows entering the value to insert the at the user-defined location. And the scanf statement assigns it to the Value variable

printf("\nPlease Enter the value of an Array Emenent to insert\n");
scanf("%d", &Value);

From the above c program to insert an element in an array example screenshot, you can observe that

Array Size = 4 and Elements of an Array are {10, 20, 30, 40}. This means
Array[0] = 10
Array[1] = 20
Array[2] = 30
Array[3] = 40

Position of an Element = 2, and User Entered Value is 89

for (i = Number - 1; i >= Position - 1; i--)
   {
	     Array[i+1] = Array[i];
   }

For Loop – First Iteration
i = Number–1 means i will initialize to 3
i=3, position = 2

Condition inside the For loop (i >= Position – 1) is True because (3 is greater than 1)
Array[i+1] = Array[i];
Array[4] = Array[3]
Array[4] = 40

After the statement executed, i will be decremented to 1 because we used Decrement Operator in For Loop.

Second Iteration: After decrementing, i become 2. So, i = 2, position = 2
Condition inside the For loop (i >= Position – 1) is True because (2 is greater than 1)
Array[3] = Array[2]
Array[3] = 30

Third Iteration: After decrementing, i value become 1. So, i = 1, position = 2
Condition inside the For loop (i >= Position – 1) is True because (1 is Equal to 1)
Array[2] = Array[1]
Array[2] = 20

After decrementing, i become 0. It means the condition inside the For loop will fail. So, the C program compiler will exit from the for loop.

The final array of the c program to insert an element in an array program will be: Array[4] = {10, 20, 20, 30, 40}

Now we have to insert the new element at the position 2. The below statement will do the trick for you
Array[Position-1] = Value;
Array[2-1] = 89;

Next for loop,

for (i = 0; i <= Number; i++)
  {
 	printf("%d\t", Array[i]);
  }

Will traverse all the elements in the Array as we explained above and, it will display the values inside the Array[i] array using the printf statements array.

C Program to insert an Element in an Array using While Loop

This program allows the user to enter the array size, Elements of an Array, Location of an element, and the value of an element. We are using While Loop to insert the value in an Array at the user-specified location.

#include <stdio.h>
 
int main()
{
  int Array[10], Position, i, Number, Value;
 
  printf("\nPlease Enter Number of elements in an array\n");
  scanf("%d", &Number);
 
  printf("\nPlease Enter %d elements of an Array \n", Number);
  for (i = 0; i < Number; i++)
   {
     scanf("%d", &Array[i]);
   }     
 
  printf("\nPlease Enter the location of a Element you want to insert\n");
  scanf("%d", &Position);
 
  printf("\nPlease Enter the value of an Array Emenent to insert\n");
  scanf("%d", &Value);
 
  i = Number - 1;
  while(i >= Position - 1)
   {
	 Array[i+1] = Array[i];
	 i--;
   }
 Array[Position-1] = Value;
  
 printf("\n Final Array after Inserting an  Elemnt is:\n");
 for (i = 0; i <= Number; i++)
  {
 	printf("%d\t", Array[i]);
  }     
 
 return 0;
}

We just replaced the For Loop with the While Loop, but the functionality is the same as the above c program to insert an element in an array example. Please refer to While Loop in C article to understand the While Loop Functionality.

Please Enter Number of elements in an array
7

Please Enter 7 elements of an Array 
10 20 30 40 50 60 70

Please Enter the location of a Element you want to insert
5

Please Enter the value of an Array Emenent to insert
987

 Final Array after Inserting an  Elemnt is:
10	20	30	40	987	50	60	70	

Comments are closed.