C program to Insert an Element in an Array

«
»
Problem Description

Write a C Program to insert an element in an Array.

What is Array?

An array is a collection of similar data elements stored in a contiguous memory location.

Example: arr[5] = {2,7,1,23,5}

Array example with value and index.

The new element we are entering here is 15 and the position where the element must be inserted is “4”.

Insert a Array Element at Specified Position

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
Problem Solution

1. Declare a one-dimensional array of some fixed capacity.
2. Take size of the array as input from users
3. Define array elements, taking each element as input from users.
4. Get the number to be inserted.
5. Get the position where element needs to be inserted.
6. From that position shift all elements to one position forward.
7. Insert the element in that position.
8. Exit.

Program/Source Code

Here is the source code of the C program to insert an element into an Array. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C program to insert an element in a specified position in a given array
  3.  */
  4.  
  5. #include <stdio.h>
  6. void main()
  7. {
  8.     int array[100];
  9.     int i, n, x, pos;
  10.  
  11.     printf("Enter the number of elements in the array \n");
  12.     scanf("%d", &n);
  13.     printf("Enter the elements \n");
  14.  
  15.     for (i = 0; i < n; i++)
  16.     {
  17.         scanf("%d", &array[i]);
  18.     }
  19.  
  20.     printf("Input array elements are: \n");
  21.     for (i = 0; i < n; i++)
  22.     {
  23.         printf("%d ", array[i]);
  24.     }
  25.     printf("\nEnter the new element to be inserted: ");
  26.     scanf("%d", &x);
  27.     printf("Enter the position where element is to be inserted: ");
  28.     scanf("%d", &pos);
  29.  
  30.     //shift all elements 1 position forward from the place
  31.     //where element needs to be inserted
  32.     n=n+1;
  33.     for(i = n-1; i >= pos; i--)
  34.         array[i]=array[i-1];
  35.  
  36.     array[pos-1]=x; //Insert the element x on the specified position
  37.  
  38.     //print the new array
  39.     for (i = 0; i < n; i++)
  40.     {
  41.         printf("%d ", array[i]);
  42.     }
  43. }
Program Explanation

1. Initialize an array of size 100.
2. Ask the user for number of elements they want to enter into the array. Store it in variable n.
3. Take n number of elements as input from the user and store it in the array.
4. Ask the user for new element, x to be inserted and the position pos where element needs to be inserted.
5. Increment the value of n by 1.
5. Shift all the elements that are next to pos to one index forward.
6. Now, array[pos] is empty, insert the element x into the array.
7. Print all the elements.

Example:

  • Consider an example with n=5 and array elements as 2, 7, 1, 23, 5.
  • Now ask the user for x and pos. Suppose the value entered by user are x=15 and pos=4.
  • Increment the value of n by 1 i.e., n=6. Run a for loop from i=n-1 = 5 to i=pos = 4 and in each iteration insert the element at previous index i.e., i-1 to index i. Therefore, array[5] = array[4] which is equal to 5.
  • Decrement the value of i by 1 and repeat the same process array[4] = array[3] i.e., array[4] = 23.
  • Now i=3 and pos=4 so condition in for loop becomes false, come out of the loop.
  • Now, insert the element x in index pos-1 i.e., array[3] = 15.
  • The element is inserted now, run a for loop and print the array with element being inserted. i.e. 2 7 1 15 23 5

Time Complexity: O(n)
The above program for inserting an element in an array has a time complexity of O(n), as the for loop runs from 0 to n.

advertisement

Space Complexity: O(n)
In the above program, space complexity is O(n) as an array of size n has been initialized to store the values in it.

Runtime Test Cases

In this case, the new element is 15 and the position where the element to be inserted is “4”.

Enter the number of elements in the array
5
Enter the elements
2 7 1 23 5
Input array elements are:
2 7 1 23 5
Enter the new element to be inserted: 15
Enter the position where element is to be inserted: 4
2 7 1 15 23 5

To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.

advertisement

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.