C++ program to insert an element to an array at any position

C++ program to insert an element in an array:

In this post, we will learn how to insert an element in an array in C++. We can add one element to the start of an array, to the end of an array or at any specific positions. Let’s learn how to insert an element at various positions of an array with example.

Example 1: C++ program to insert an element at the start of an array:

To insert an element at the start, we will have to shift all other elements one index to right. Let’s learn how to do that in code:

#include <iostream>
using namespace std;

void printArray(int arr[], int size)
{
    for (int i = 0; i < size; i++)
    {
        cout << arr[i] << " ";
    }
}

int main()
{
    int size;
    cout << "Enter the size of the array: " << endl;
    cin >> size;

    int numArray[size + 1];

    for (int i = 0; i < size; i++)
    {
        cout << "Enter the element for position " << i << " :" << endl;
        cin >> numArray[i];
    }

    cout << "Given Array: " << endl;
    printArray(numArray, size);
    cout << endl;

    int firstElement;
    cout << "Enter the element to add to the first position: " << endl;
    cin >> firstElement;

    for (int i = size - 1; i >= 0; i--)
    {
        numArray[i + 1] = numArray[i];
    }

    numArray[0] = firstElement;

    cout << "Final Array: " << endl;
    printArray(numArray, size + 1);
    cout << endl;
}

Here,

  • The size of the array is assigned to the integer variable size.
  • numArray is the array to hold the user input values. It is created with a size one more than the user input size.
  • The printArray method is used to print the content of an array.
  • It uses one for loop to read the array contents from the user and add these numbers to the array.
  • The second for loop is used to shift all numbers one index right. Once all are shifted, the new number is inserted to the first index, i.e. index 0.

If you run this program, it will print output as below:

Enter the size of the array: 
4
Enter the element for position 0 :
1
Enter the element for position 1 :
2
Enter the element for position 2 :
3
Enter the element for position 3 :
4
Given Array: 
1 2 3 4 
Enter the element to add to the first position: 
5
Final Array: 
5 1 2 3 4 

Example 2: C++ program to insert an element at the end of an array:

It is easy to insert an element to the end of a given array. We don’t have to shift the elements. We can simply add it to the end.

#include <iostream>
using namespace std;

void printArray(int arr[], int size)
{
    for (int i = 0; i < size; i++)
    {
        cout << arr[i] << " ";
    }
}

int main()
{
    int size;
    cout << "Enter the size of the array: " << endl;
    cin >> size;

    int numArray[size + 1];

    for (int i = 0; i < size; i++)
    {
        cout << "Enter the element for position " << i << " :" << endl;
        cin >> numArray[i];
    }

    cout << "Given Array: " << endl;
    printArray(numArray, size);
    cout << endl;

    int lastElement;
    cout << "Enter the element to add to the last position: " << endl;
    cin >> lastElement;

    numArray[size] = lastElement;

    cout << "Final Array: " << endl;
    printArray(numArray, size + 1);
    cout << endl;
}

In this example, we are adding the last element to the end of the array. We are simply adding it to the index position size.

Enter the size of the array: 
4
Enter the element for position 0 :
1
Enter the element for position 1 :
2
Enter the element for position 2 :
3
Enter the element for position 3 :
4
Given Array: 
1 2 3 4 
Enter the element to add to the last position: 
5
Final Array: 
1 2 3 4 5 

Example 3: C++ program to insert an element at any specific position in an array:

Let’s change the above example to insert an element at any specific position of an array. We have to shift all elements one position right starting from that position to end. Once the shifting is done, we can insert the element at that position.

#include <iostream>
using namespace std;

void printArray(int arr[], int size)
{
    for (int i = 0; i < size; i++)
    {
        cout << arr[i] << " ";
    }
}

int main()
{
    int size;
    cout << "Enter the size of the array: " << endl;
    cin >> size;

    int numArray[size + 1];

    for (int i = 0; i < size; i++)
    {
        cout << "Enter the element for position " << i << " :" << endl;
        cin >> numArray[i];
    }

    cout << "Given Array: " << endl;
    printArray(numArray, size);
    cout << endl;

    int element, index;
    cout << "Enter the index to insert the new element: " << endl;
    cin >> index;

    cout << "Enter the element to add to this index: " << endl;
    cin >> element;

    for (int i = size - 1; i >= index; i--)
    {
        numArray[i + 1] = numArray[i];
    }

    numArray[index] = element;

    cout << "Final Array: " << endl;
    printArray(numArray, size + 1);
    cout << endl;
}

This is similar to the first example. The difference is that we are reading the index and the element to insert at this index. The second for loop shifts all numbers one step right from index to the end of the array. It will give output as below:

Enter the size of the array: 
5
Enter the element for position 0 :
1
Enter the element for position 1 :
2
Enter the element for position 2 :
3
Enter the element for position 3 :
4
Enter the element for position 4 :
5
Given Array: 
1 2 3 4 5 
Enter the index to insert the new element: 
2
Enter the element to add to this index: 
10
Final Array: 
1 2 10 3 4 5 

You might also like: