codescracker
c++

C++ Program to Insert an Element in an Array



« Previous Program Next Program »


In this article, you will learn and get code to insert an element in an array in C++ language. Here are the list of programs available in this article:

Insert Element at the End of an Array

This program asks from user to enter 5 numbers or elements for an array and then further ask to enter the element to insert it at the end of given array.

The question is, write a program in C++ to insert an element at the end of an array. Here is its answer:

// C++ Program to Insert Element at End of Array
// ----codescracker.com----

#include<iostream>
using namespace std;
int main()
{
    int arr[6], i, elem;
    cout<<"Enter 5 Array Elements: ";
    for(i=0; i<5; i++)
        cin>>arr[i];
    cout<<"\nEnter Element to Insert: ";
    cin>>elem;
    arr[i] = elem;
    cout<<"\nThe New Array is:\n";
    for(i=0; i<6; i++)
        cout<<arr[i]<<"  ";
    cout<<endl;
    return 0;
}

This program was build and run under Code::Blocks IDE. Here is its initial output:

insert element at end of array c++

Now supply 5 inputs as 5 numbers or elements for the array say 4, 5, 6, 7, 8 and then enter a number or element say 9 to insert in the array. Here is the sample output produced by above program after providing these inputs:

c++ insert element in array

This programs receives 5 array elements using for loop, one by one. That is, if user enters 5 array elements as 4, 5, 6, 7, 8, then these elements gets stored in arr[] in following way:

After receiving all the 5 elements for the array, the value of i is 5 now. So just receive another input, that is the element to insert and initialize it to arr[i] or arr[5]. Finally print the new array.

Insert Element in Array at a Specific Position

To insert an element in an array in C++ programming, you have to ask from user to enter the size and elements for the array. And then ask to enter the element to insert and at what position as shown in the program given below:

After inserting the element at desired position, don't forgot display the new array on the screen.

// C++ Insert Element at Specific Position
// ----codescracker.com----

#include<iostream>
using namespace std;
int main()
{
    int arr[50], i, elem, pos, tot;
    cout<<"Enter the Size for Array: ";
    cin>>tot;
    cout<<"Enter "<<tot<<" Array Elements: ";
    for(i=0; i<tot; i++)
        cin>>arr[i];
    cout<<"\nEnter Element to Insert: ";
    cin>>elem;
    cout<<"At What Position ? ";
    cin>>pos;
    for(i=tot; i>=pos; i--)
        arr[i] = arr[i-1];
    arr[i] = elem;
    tot++;
    cout<<"\nThe New Array is:\n";
    for(i=0; i<tot; i++)
        cout<<arr[i]<<"  ";
    cout<<endl;
    return 0;
}

Here is its sample run with following user input:

After supplying these inputs, here is the snapshot that shows all the operations that has been done on the output screen:

C++ program insert element in array

In above program, with the help of pos's value (desired position where user wants to insert the new element), we've shifted all the element from this position to one index forth (forward). After doing this task, the previous element at this position gets moved to its one index forth. Therefore, we're free to initialize the new element at this position

Don't forgot to increment the value of tot (size of array) after inserting the new element to it. Now print the new array that contains newly inserted element at desired or specific position as shown in the output given above.

For example, the dry run of following block of code:

for(i=tot; i>=pos; i--)
    arr[i] = arr[i-1];
arr[i] = elem;

with same user input as provided in above sample run, goes like:

Same Program in Other Languages


« Previous Program Next Program »