Skip to content

Insert an element in an Array: Program, Algorithm and Flowchart

Insert operation in array

In this article, you’ll learn how to insert an element in an array at a specific position in C/C++, along with that you’ll also see the algorithm and flowchart to insert an element in an array.

So, Let’s get started!

Prerequisite: You should have the knowledge of Traverse Operation in Array.

What is insertion operation in array?

Insert operation in Array or inserting elements in an Array simply means, Adding one or more elements at specified index/indices to the existing elements of an array.

How do you insert an element in an array at a given position?

Let’s see how you can insert an element in an array at a given position step-by-step:

Variables we are using here:

  1. arr : Name of the array.
  2. size : Size of the array (i.e., total number of elements in the array)
  3. i : Loop counter or counter variable for the for loop.
  4. x : The data element to be insert.
  5. pos : The position where we wish to insert the element.

The following algorithm inserts a data element x into a given position pos (position specified by the programmer) in a linear array arr.

Algorithm to Insert an element in an Array:

  • Step 01: Start
  • Step 02: [Reset size of the array. ] set size = size + 1
  • Step 03: [Initialize counter variable. ] Set i = size - 1
  • Step 04: Repeat Step 05 and 06 for i = size -1 to i >= pos -1
  • Step 05: [Move ith element forward. ] set arr[i+1] = arr[i]
  • Step 06: [Decrease counter. ] Set i = i - 1
  • Step 07: [End of step 04 loop. ]
  • Step 08: [Insert element. ] Set arr[pos-1] = x
  • Step 09: Stop

In the above Algorithm, Step 2 to step 6 creates an empty space in the array by moving forward one location each element from the position on.

Now, few questions might be arising in your mind.

Why we are increasing the size of the array by one?

And,

Why we need to create an empty space for adding a new element in the array? Right?

The answer of the above two questions are very straightforward,

It’s only because, When you declare and initializes the values (elements) of an array, all the elements occupies its memory space according to the sequence (order) in which they are initialized.

And, By increasing the size of the array by one you basically creates a room (space) for the new incoming element which you wish to insert at a specified position in the given array.

It is obvious that the size of the empty space would be same as that of the size of each data item of the array.

Means, If the data type of the element of the array is int then, the space must be of the size of the int data type. And, If the data item will be float then, the space must be of float type.

📝 Note: The more elements you insert in the array, the more empty spaces you require.

So, If you wish to insert two elements in a given array, then you need two empty spaces, where each space must be equal to the size of each element of the array and so on…

After that, We are moving all the elements from last of the array by one in forward direction until we reach to the position where we wish to insert the element.

Here, We maintain these elements are moved in reverse order (i.e., first arr[size-1], then arr[size-2], …, ) and at last the arr[pos-1]; otherwise data might be erased.

We first set i = size -1 and then, using i as a counter variable, decrease i each time the loop is executed until i reaches pos-1.

The next step, Step 7, ends the loop.

And, at the last step, Step 8, inserts x into the array in the empty space just created.

Visual Representation:

Insert an element in an array

Flowchart to Insert an element in an Array:

Insert an element in an array

Implementation in C:

// writing a program in C to insert an element in an array

void main()
{
     int i, size, x, pos;
     int arr[]={2, 4, 6, 8, 12};
     size=sizeof(arr)/sizeof(arr[0]);
     printf("The array elements before insertion operation:\n");
     for(i=0;i<size;i++)
        printf("arr[%d] = %d\n",i, arr[i]);
     printf("Enter the element to be insert: ");
     scanf("%d", &x);
     printf("Enter the position where you want to insert the element: ");
     scanf("%d", &pos);
     size = size + 1;
     printf("The array elements after insertion operation:\n");
     for(i=size-1;i>=pos-1;i--)
        arr[i+1]=arr[i];
     arr[pos-1]=x;
     for(i=0;i<size;i++)
        printf("arr[%d] = %d\n",i, arr[i]);
}

If you compile and run the above program, it will produce the following result:

Output:

The array elements before insertion operation:
arr[0] = 2
arr[1] = 4
arr[2] = 6
arr[3] = 8
arr[4] = 12
Enter the element to be insert: 10
Enter the position where you want to insert the element: 5
The array elements after insertion operation:
arr[0] = 2
arr[1] = 4
arr[2] = 6
arr[3] = 8
arr[4] = 10
arr[5] = 12


Implementation in C++:

// writing a program in C++ to insert an element in an array

#include <iostream>
using namespace std;
int main()
{
     int i, size, x, pos;
     int arr[]={2, 4, 6, 8, 12};
     size=sizeof(arr)/sizeof(arr[0]);
     cout << "The array elements before insertion operation:\n";
     for(i=0;i<size;i++)
        cout << "arr[" << i << "] = " << arr[i] << "\n";
     cout << "Enter the element to be insert: ";
     cin >> x;
     cout << "Enter the position where you want to insert the element: ";
     cin >> pos;
     size = size + 1;
     cout << "The array elements after insertion operation: ";
     for(i=size-1;i>=pos-1;i--)
        arr[i+1]=arr[i];
     arr[pos-1]=x;
     for(i=0;i<size;i++)
        cout << "\narr[" << i << "] = " << arr[i];
     return 0;
}

If you compile and run the above program, it will produce the following result:

Output:

The array elements before insertion operation:
arr[0] = 2
arr[1] = 4
arr[2] = 6
arr[3] = 8
arr[4] = 12
Enter the element to be insert: 786
Enter the position where you want to insert the element: 1
The array elements after insertion operation:
arr[0] = 786
arr[1] = 2
arr[2] = 4
arr[3] = 6
arr[4] = 8
arr[5] = 12


Conclusion:

In this article, you learned how to insert an element in an array at a specific position in C/C++, you also became familiar with the algorithm and flowchart to insert an element in an array.

So,

I really hope that you liked my article and got a lot of value from it.

Now, It’s your turn!

If you have any question, feel free to ask in the comment section below right now.


Share this Post

Leave a Reply

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