C++ Programming Code Examples C++ > Arrays and Matrices Code Examples C++ Programming Code to Merge Two Arrays C++ Programming Code to Merge Two Arrays To merge two arrays in C++ programming, you have to ask to the user to enter the array 1 size and elements then array 2 size and elements to merge both the array and store the merged result in the third array say merge[ ]. So to merge two array, start adding the element of first array to the third array (target array) after this start appending the elements of second array to the third array (target array) as shown here in the following program. 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). You can also sort the two array then merge or sort after merge. So to learn about sorting, here are the three techniques : /* C++ Program - Merge Two Arrays */ #include<iostream.h> #include<conio.h> void main() { clrscr(); int arr1[50], arr2[50], size1, size2, size, j, k, merge[100]; cout<<"Enter Array 1 Size : "; cin>>size1; cout<<"Enter Array 1 Elements : "; for(j=0; j<size1; j++) { cin>>arr1[j]; } cout<<"Enter Array 2 Size : "; cin>>size2; cout<<"Enter Array 2 Elements : "; for(j=0; j<size2; j++) { cin>>arr2[j]; } for(j=0; j<size1; j++) { merge[j]=arr1[j]; } size=size1+size2; for(j=0, k=size1; k<size && j<size2; j++, k++) { merge[k]=arr2[j]; } cout<<"Now the new array after merging is :\n"; for(j=0; j<size; j++) { cout<<merge[j]<<" "; } getch(); }