codescracker
c++

C++ Program to Merge Two Arrays



« Previous Program Next Program »

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

Merge Two Arrays in C++

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

Note - At the time of receiving array's elements, we've applied the merge operation. That is, elements received by user gets initialized to a third array one by one.

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

// C++ Program to Merge Two Arrays
// ---codescracker.com---

#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 build and run under Code::Blocks IDE. Here is its sample run:

C++ program merge two array

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

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

c++ merge two arrays

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

Merge Two Arrays in Ascending Order

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

// C++ Merge Two Arrays in Ascending Order
// ----codescracker.com----

#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 following user inputs:

After supplying these inputs, here is the output produced by 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 previous program):

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

with

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

Note - Just a matter of greater than and less than symbol, whole program gets changed.

Rest of the things will be same. That is, the code that sorts merged array in ascending order in previous program, has changed to sort the same array in descending order.

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

// C++ Merge Two Arrays in Descending Order
// ----codescracker.com----

#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 following user inputs:

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

merge two arrays in descending order c++

Same Program in Other Languages

C++ Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker