on Leave a Comment

C++ Program to Merge two Arrays

This c++ program merge two arrays in a third array. First you have to enter the size and elements of two arrays, and then both arrays are merged using for loop.

C++ Program to Merge two Arrays

#include<iostream>
using namespace std;
int main()
{
    int arr1[50], arr2[50], arr3[100], n1, n2, i;
    cout<<"Enter Size of Array 1: ";
    cin>>n1;
    cout<<"Enter Elements of Array 1: ";
    for(i=0; i<n1; i++)
        cin>>arr1[i];
    cout<<"\nEnter Size of Array 2: ";
    cin>>n2;
    cout<<"Enter Elements of Array 2: ";
    for(i=0; i<n2; i++)
        cin>>arr2[i];
    for(i=0 ; i<n1; i++)
        arr3[i] = arr1[i];
    for(int j=0, i=n1; i<n1+n2; i++, j++)
        arr3[i]=arr2[j];
    cout<<"\nMerged Array is: ";
    for(i=0; i<n1+n2; i++)
        cout<<arr3[i]<<"  ";
    return 0;
}

OUTPUT:

Enter Size of Array 1: 3
Enter Elements of Array 1: 1 2 3

Enter Size of Array 2: 3
Enter Elements of Array 2: 4 5 6

Merged Array is: 1  2  3  4  5  6

See also:




0 comments:

Post a Comment