C Program To Insert New Element At Specified Position of An Array

Write a C program to insert new element/number at specified position of an array. The array elements need not be in sorted order.

Related Read:
C Program To Shift Elements of An Array by n Position

Example: Expected Input/Output

Enter 10 integer numbers
1
2
3
4
5
6
8
9
10
11
Enter the position where new number has to be inserted
6
Enter a new number to be inserted at position 6
7
Array after inserting 7 at position 6
1
2
3
4
5
6
7
8
9
10
11

Visual Representation

insert element at specified position of an array

Video Tutorial: C Program To Insert New Element At Specified Position of An Array


[youtube https://www.youtube.com/watch?v=oT31byliQPE]

YouTube Link: https://www.youtube.com/watch?v=oT31byliQPE [Watch the Video In Full Screen.]

Source Code: C Program To Insert New Element At Specified Position of An Array

#include<stdio.h>
#define N 11

int main()
{
    int a[N], i, pos, num;

    printf("Enter %d integer numbers\n", (N - 1));
    for(i = 0; i < (N -1); i++)
        scanf("%d", &a[i]);

    printf("Enter the position where new number has to be inserted\n");
    scanf("%d", &pos);

    if(pos < N)
    {
        printf("Enter a new number to be inserted at position %d\n", pos);
        scanf("%d", &num);
        for(i = N - 1; i > pos; i--)
                a[i] = a[i - 1];

        a[pos] = num;

        printf("Array after inserting %d at position %d\n", num, pos);
        for(i = 0; i < N; i++)
            printf("%d\n", a[i]);
    }
    else
    {
        printf("Please enter a position within the range/size of the array!\n");
    }

    printf("\n");

    return 0;
}

Output:
Enter 10 integer numbers
11
25
52
36
98
92
45
69
59
2
Enter the position where new number has to be inserted
5
Enter a new number to be inserted at position 5
100
Array after inserting 100 at position 5
11
25
52
36
98
100
92
45
69
59
2

Logic To Insert New Element At Specified Position of An Array

We ask the user to enter (N – 1) number of elements and store it inside array variable a[N]. We leave the last index position empty(technically it’ll automatically have zero as the element). Now we ask the user to enter the position where he or she wants to insert new number/element. Next we ask the user to input the new number to be inserted at the specified position. Once we get the position and the new number, we start the “for loop”.

We initialize i with last index of the array, which is N – 1. We iterate the array until i is greater than the user entered position.

Note: We iterate the for loop only until i is greater than user specified position because, we need to shift the elements to right by 1 position only after the position/index indicated by the user. We won’t move any elements above the user specified position.

Inside for loop
Inside for loop we move the elements by 1 position to the bottom or right. Ex: if i value is 5, then a[5] will be assigned whatever the value is present in it’s previous index a[4].

        for(i = N - 1; i > pos; i--)
                a[i] = a[i - 1];

        a[pos] = num;

Once all the elements from user input position move 1 position right/down, we insert the new element/number at user specified position.

Explanation With Example

If a[6] = {5, 7, 3, 2, 1};
Position to insert new element/number: 2
New number to be inserted: 9

        for(i = N - 1; i > pos; i--)
                a[i] = a[i - 1];

        a[pos] = num;

We initialize i to last index of the array, which is N – 1. Since N is 6 in this case, 6 -1 is 5. So i is initialized to 5. We’ve not assigned any element/value to a[5]. Technically it’ll have 0.

iposa[i]a[i – 1]a[i] = a[i – 1]
52a[5] = 0a[4] = 1a[5] = 1
42a[4] = 1a[3] = 2a[4] = 2
32a[3] = 2a[2] = 3a[3] = 3
22

Now that index i is 2 and user input position is also 2. So 2 > 2 returns false, so the control exits the for loop. Outside for loop we’ve a[pos] = num. So the position is 2 and the new number to be inserted is 9. i.e., a[2] = 9.

So the array elements after execution of above logic:
a[6] = {5, 7, 9, 3, 2, 1};

That’s how we successfully inserted new element/number 9 at position 2.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert