C Program to Insert an Element in an Array

C program to insert an element in an array | Here we will write a C program to insert a new element entered from the user at a particular position in a given list of numbers.

Example1:-
Array = {10, 25, 15, 14, 30}
Position = 2
Element = 6
Resultant array = {10, 25, 6, 15, 14, 30}

Note:- Array index starts from 0, not from the 1.

Example2:-
Array = {10, 25, 15, 14, 30}
Position = 8
Element = 6
Error! Position is not valid.
Enter position from 0 to 5.

The position from 0 to the length of array are valid. If the position is 0 then element will be inserted at the starting of the array and if the position = length of original array then element will be inserted at the end of the array.

We can’t insert an element when the position is lesser than 0 because the array index always starts with 0. When the new element position is greater than the last position+1 of the array then again we can’t insert the element.

Approach to insert an element in an array,

  1. Take an array, position, and element.
  2. Check position is valid or not. If the position is invalid then don’t go to the next step.
  3. Create a new array with the size of n+1, where n is the size of the original array.
  4. Copy the element of the original array to the new array from 0 to position-1 index.
  5. Insert an element at position index.
  6. Copy remaining elements of the original array to the new array.

C Program to Insert an Element in the given Array

#include<stdio.h>
int main()
{
  // original array
  int arr[] = {10, 20, 30, 40, 50};

  // take position and element
  int index, key;
  printf("Enter position: ");
  scanf("%d", &index);
  printf("Enter element to insert: ");
  scanf("%d", &key);

  // calculate size of the array
  int n = sizeof(arr)/sizeof(arr[0]);

   // check position
  if(index < 0 || index > n)
  {
    printf("Error! The position is not valid.");
    printf("\nPlease, Enter position from 0 to %d\n", n);
    return 0;
  }

  // create new array of size = n+1
  int temp[n+1];

  // copy elements
  for (int i=0, j=0; i <= n; ++i)
  {
    if(i == index) temp[i] = key;
    else temp[i] = arr[j++];
  }
  
  // display new array
  printf("Array elements are: \n");
  for (int i = 0; i <= n; ++i)
  {
    printf("%d ", temp[i]);
  }

  return 0;
}

Output:-

Enter position: 0
Enter element to insert: 55
Array elements are:
55 10 20 30 40 50

Enter position: 5
Enter element to insert: 99
Array elements are:
10 20 30 40 50 99

Enter position: 9
Enter element to insert: 100
Error! The position is not valid.
Please, Enter position from 0 to 5

Enter position: -2
Enter element to insert: 10
Error! The position is not valid.
Please, Enter position from 0 to 5

Also see:- C program to find the sum of elements in an array

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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