C++ Program to Merge Two Arrays

In this article, you will learn and get code to merge two arrays entered by the user at run-time in the C++ language. Here is the list of programs on the merging of two arrays available in this article:

Merge two arrays in C++

To merge two arrays in C++ programming, you have to ask the user to enter the sizes and elements for both arrays. Then merge these two given arrays into a third array, as shown in the program given below:

Note: At the time of receiving the array's elements, we applied the merge operation. That is, the elements received from the user are added one by one to a third array.

The question is, "Write a program in C++ to merge two arrays." Here is its answer:

#include<iostream>
using namespace std;
int main()
{
    int arrOne[50], arrTwo[50], arrMerge[100];
	int sizeOne, sizeTwo, i, k;
    cout<<"Enter the Size for First Array: ";
    cin>>sizeOne;
    cout<<"Enter "<<sizeOne<<" Elements for First Array: ";
    for(i=0; i<sizeOne; i++)
    {
        cin>>arrOne[i];
        arrMerge[i] = arrOne[i];
    }
    k = i;
    cout<<"\nEnter the Size for Second Array: ";
    cin>>sizeTwo;
    cout<<"Enter "<<sizeTwo<<" Elements for Second Array: ";
    for(i=0; i<sizeTwo; i++)
    {
        cin>>arrTwo[i];
        arrMerge[k] = arrTwo[i];
        k++;
    }
    cout<<"\nThe New Array (Merged Array):\n";
    for(i=0; i<k; i++)
        cout<<arrMerge[i]<<" ";
    cout<<endl;
    return 0;
}

This program was built and runs under the Code::Blocks IDE. Here is its sample run:

C++ program merge two array

Now supply the inputs one by one. For example, if the user enters:

then here is the output produced by the above program after supplying these inputs and pressing the ENTER key:

c++ merge two arrays

You can also sort the two arrays, then merge. Alternatively, sort after merging. Here is the list of sorting techniques you can go with:

Merge Two Arrays in Ascending Order

This program merges two arrays entered by the user at run-time in ascending order. That is, the two arrays get merged and then sorted before printing the merged array on output.

#include<iostream>
using namespace std;
int main()
{
    int arrOne[50], arrTwo[50], arrMerge[100];
    int sizeOne, sizeTwo, sizeMerge, i, j, temp;
    cout<<"Enter the Size for First Array: ";
    cin>>sizeOne;
    cout<<"Enter the Size for Second Array: ";
    cin>>sizeTwo;
    cout<<"\nEnter "<<sizeOne<<" Elements for First Array: ";
    for(i=0; i<sizeOne; i++)
        cin>>arrOne[i];
    cout<<"\nEnter "<<sizeTwo<<" Elements for Second Array: ";
    for(i=0; i<sizeTwo; i++)
        cin>>arrTwo[i];
    // merging the two arrays
    for(i=0; i<sizeOne; i++)
    {
        arrMerge[i] = arrOne[i];
    }
    for(j=0; j<sizeTwo; j++)
    {
        arrMerge[i] = arrTwo[j];
        i++;
    }
    sizeMerge = i;
    // sorting the merged array in ascending order
    for(j=0; j<(sizeMerge-1); j++)
    {
        for(i=0; i<(sizeMerge-1); i++)
        {
            if(arrMerge[i]>arrMerge[i+1])
            {
                temp = arrMerge[i];
                arrMerge[i] = arrMerge[i+1];
                arrMerge[i+1] = temp;
            }
        }
    }
    cout<<"\nThe New Array (Merged Array):\n";
    for(i=0; i<sizeMerge; i++)
    {
        if(i==(sizeMerge-1))
            cout<<arrMerge[i];
        else
            cout<<arrMerge[i]<<" ";
    }
    cout<<endl;
    return 0;
}

Here is its sample run, with the following user inputs:

After supplying these inputs, here is the output produced by the above program:

merge two arrays in ascending order c++

Merge two arrays in descending order

To merge two arrays in descending order, change the following code (from the previous program):

if(arrMerge[i]>arrMerge[i+1])

with

if(arrMerge[i]<arrMerge[i+1])

It's worth noting that changing the greater-than and less-than symbols changes the entire program.

Everything else will remain the same.That is, the code that sorted merged arrays in ascending order in the previous program has changed to sort the same arrays in descending order.

But still, if you want the complete program on the merging of two arrays in descending order, here is the source code for you:

#include<iostream>
using namespace std;
int main()
{
    int arrOne[50], arrTwo[50], arrMerge[100];
    int sizeOne, sizeTwo, sizeMerge, i, j, temp;
    cout<<"Enter the Size for First Array: ";
    cin>>sizeOne;
    cout<<"Enter the Size for Second Array: ";
    cin>>sizeTwo;
    cout<<"\nEnter "<<sizeOne<<" Elements for First Array: ";
    for(i=0; i<sizeOne; i++)
        cin>>arrOne[i];
    cout<<"\nEnter "<<sizeTwo<<" Elements for Second Array: ";
    for(i=0; i<sizeTwo; i++)
        cin>>arrTwo[i];
    // merging the two arrays
    for(i=0; i<sizeOne; i++)
    {
        arrMerge[i] = arrOne[i];
    }
    for(j=0; j<sizeTwo; j++)
    {
        arrMerge[i] = arrTwo[j];
        i++;
    }
    sizeMerge = i;
    // sorting the merged array in descending order
    for(j=0; j<(sizeMerge-1); j++)
    {
        for(i=0; i<(sizeMerge-1); i++)
        {
            if(arrMerge[i]<arrMerge[i+1])
            {
                temp = arrMerge[i];
                arrMerge[i] = arrMerge[i+1];
                arrMerge[i+1] = temp;
            }
        }
    }
    cout<<"\nThe New Array (Merged Array in Descending Order):\n";
    for(i=0; i<sizeMerge; i++)
    {
        if(i==(sizeMerge-1))
            cout<<arrMerge[i];
        else
            cout<<arrMerge[i]<<" ";
    }
    cout<<endl;
    return 0;
}

Here is its sample run with the following user inputs:

The snapshot given below shows the sample output produced by the above program after supplying these inputs:

merge two arrays in descending order c++

The same program in different languages

C++ Quiz


« Previous Program Next Program »


Liked this post? Share it!