Following C++ program ask to the user to enter array 1 and 2 size, then ask to enter array 1 and 2 elements, to merge or add to form new array, then display the result of the added array or merged array (Here we display the direct merged array). Test Case 1 Input (stdin) 5 2 3 5 6 1 3 4 6 2 Expected Output 2 3 5 6 1 4 6 2 Test Case 2 Input (stdin) 4 6 7 2 3 5 2 1 3 4 7 Expected Output 6 7 2 3 2 1 3 4 7
/* C++ Program - Merge Two Arrays */
#include<iostream>
using namespace std;
int main()
{
int arr1[50], arr2[50], size1, size2, size, i, j, k, merge[100];
//cout<<"Enter Array 1 Size : ";
cin>>size1;
//cout<<"Enter Array 1 Elements : ";
for(i=0; i<size1; i++)
{
cin>>arr1[i];
}
//cout<<"Enter Array 2 Size : ";
cin>>size2;
//cout<<"Enter Array 2 Elements : ";
for(i=0; i<size2; i++)
{
cin>>arr2[i];
}
for(i=0; i<size1; i++)
{
merge[i]=arr1[i];
}
size=size1+size2;
for(i=0, k=size1; k<size && i<size2; i++, k++)
{
merge[k]=arr2[i];
}
//cout<<"Now the new array after merging is :\n";
for(i=0; i<size; i++)
{
cout<<merge[i]<<" ";
}
return 0;
}