C program to insert an element in an array

This C program inserts an element in an array after it’s created. Here, the initial array is also entered by the user and then a new element is inserted at the position entered by the user.

The size of the array will definitely increase once another element is inserted to it, therefore the size of the array must be kept in mind.

Program: C program to insert an element in an array at a given position

We’ll use the index of the array elements as the most important tool here. Below is the program to insert an element at the desired position entered by the user.

#include<stdio.h>
int main()
{
    int a[50],pos,i,n,val;
    printf("Enter no. of elements in array: ");
    scanf("%d",&n);
    printf("\nEnter the elements: ");
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
    printf("\nEnter position where you want to insert element: ");
    scanf("%d",&pos);
    if(pos > n)
        printf("\nCannot insert element at this position!!");
    else
    {
        printf("\nEnter the number to be inserted: ");
        scanf("%d",&val);
        for(i=(n-1);i>=(pos-1);i--)
            a[i+1]=a[i];
        a[pos-1]=val;
        printf("\nUpdated array after insertion:\n");
        for(i=0;i<=n;i++)
            printf("\n%d",a[i]);
    }
    printf("\n");
    return 0;
}

Output: Insert element and print the updated array

Enter no. of elements in array: 6

Enter the elements: 43 65 37 22 95 81

Enter the position where you want to insert element: 5

Enter the number to be inserted: 17 

Updated array after insertion:

43
65
37
22
17
95
81

We can also write a C program to do the same thing by using functions and pointers. Let’s try to solve this problem using functions.

Program: C program to insert an element in an array using function and pointers

#include<stdio.h>
int *insert(int[], int, int);
int main()
{
    int a[50], pos, i, n;
    printf("Enter no of elements: ");
    scanf("%d",&n);
    printf("\nEnter the elements : ");
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
    printf("\nEnter the position where you want to insert: ");
    scanf("%d",&pos);
    if(pos>n)
        printf("\nEnter valid position to insert element!!\n");
    else
        insert(a,n,pos);
    printf("\nUpdated array after insertion:\n");
        for(i=0;i<=n;i++)
            printf("\n%d",a[i]);
    printf("\n");
    return 0;
}
int *insert(int arr[], int size, int pos){
    int i, val;
    printf("\nEnter the number to be inserted: ");
    scanf("%d",&val);
    for(i=(size-1);i>=(pos-1);i--)
        arr[i+1]=arr[i];
        arr[pos-1]=val;
    return arr;
}

Output:

Enter no of elements: 5

Enter the elements : 43 54 98 29 30

Enter the position where you want to insert: 4

Enter the number to be inserted: 101

Updated array after insertion:

43
54
98
101
29
30

This is how to insert an element in an array with and without using functions and array as pointers. Similarly, you can also delete element from an array using these methods. Visit the link to learn deletion in array.

Share your code for insertion in an array in the comments.

For more programming articles and code snippets, visit programming articles.

About ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ

Linux and Python enthusiast, in love with open source since 2014, Writer at programming-articles.com, India.

View all posts by ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ →

Leave a Reply

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