- C++ Programming Examples
- C++ Programming Examples
- C++ Hello World
- C++ Get Input
- C++ Print Integer
- C++ Add Two Numbers
- C++ Add Sub Mul Div
- C++ Add Digits
- C++ Find Average Perc
- C++ Find Arithmetic Mean
- C++ Sum of n Natural Numbers
- C++ Sum of n Numbers
- C++ Area Perimeter of Square
- C++ Area Perimeter of Rectangle
- C++ Area Perimeter of Triangle
- C++ Area Circum of Circle
- C++ Find Simple Interest
- C++ Fahrenheit to Celsius
- C++ Celsius to Fahrenheit
- C++ Print Prime Numbers
- C++ Reverse a Number
- C++ Swap Two Numbers
- C++ Print Multiplication Table
- C++ Find Factorial of Number
- C++ Find Factors of Number
- C++ Find HCF & LCM
- C++ Make Calculator
- C++ Count Digits in Number
- C++ Sum of First & Last Digit
- C++ Product of Digits of Number
- C++ Sum of Squares of Digits
- C++ Interchange Digits of Number
- C++ if else Programs
- C++ Check Even or Odd
- C++ Check Prime or Not
- C++ Check Alphabet or Not
- C++ Check Vowel or Not
- C++ Check Leap Year or Not
- Check Reverse equal Original
- C++ Check Perfect Number
- C++ Check Palindrome or Not
- C++ Check Armstrong or Not
- C++ Divisibility Test
- C++ Find Wage of Labor
- C++ Find Discounted Price
- C++ Find Shipping Charge
- C++ Find Telephone Bills
- C++ Calculate Student Grade
- C++ Largest of Two Numbers
- C++ Largest of Three Numbers
- C++ Number Conversion
- C++ Decimal to Binary
- C++ Decimal to Octal
- C++ Decimal to Hexadecimal
- C++ Binary to Decimal
- C++ Binary to Octal
- C++ Binary to Hexadecimal
- C++ Octal to Decimal
- C++ Octal to Binary
- C++ Octal to Hexadecimal
- C++ Hexadecimal to Decimal
- C++ Hexadecimal to Binary
- C++ Hexadecimal to Octal
- C++ Pattern Programs
- C++ Pattern Programs
- C++ Print Diamond Pattern
- C++ Print Floyd's Triangle
- C++ Print Pascal's Triangle
- C++ Array Programs
- C++ 1D Array Program
- C++ Linear Search
- C++ Binary Search
- Find Largest Element in Array
- Find Smallest Element in Array
- Find Second Largest Element
- Find Second Smallest Element
- C++ Sum of All Elements
- C++ Multiply All Elements
- C++ Element on Even Position
- C++ Element on Odd Position
- C++ Print Even Numbers in Array
- C++ Print Odd Numbers in Array
- C++ Count Even/Odd Numbers
- C++ Sum of Even/Odd Numbers
- C++ Count Positive Negative Zero
- C++ Reverse an Array
- C++ Insert Element in Array
- C++ Delete Element from Array
- C++ Merge two Arrays
- C++ Bubble Sort
- C++ Selection Sort
- C++ Insertion Sort
- C++ Common Elements
- C++ 2D Array Programs
- C++ Add Two Matrices
- C++ Subtract Two Matrices
- C++ Transpose Matrix
- C++ Multiply Two Matrices
- C++ 3D Array Programs
- C++ String Programs
- C++ Print String
- C++ Find Length of String
- C++ Compare Two Strings
- C++ Copy String
- C++ Concatenate String
- C++ Reverse a String
- C++ Delete Vowels from String
- C++ Delete Word from String
- C++ Count Character in String
- C++ Count Word in String
- C++ Frequency of Word
- C++ Remove Spaces from String
- C++ Sort a String
- C++ Uppercase to Lowercase
- C++ Lowercase to Uppercase
- C++ Swap Two Strings
- C++ Check Anagram or Not
- C++ Capitalize All Words in String
- C++ Capitalize Specific Character
- C++ Get Numbers from String
- C++ File Programs
- C++ Read a File
- C++ Write Content to File
- C++ Append Data in File
- C++ Read & Display File
- C++ Copy a File
- C++ Merge Two Files
- Count Characters, Words in File
- C++ Capitalize All Words in File
- C++ List Files in Directory
- C++ Delete a File
- C++ Encrypt & Decrypt a File
- C++ Misc Programs
- C++ Print ASCII Value
- C++ Add Binary Numbers
- C++ Generate Random Numbers
- C++ Print Smiling Face
- C++ Days into Years, Months
- Add Two Numbers using Pointer
- C++ Print Fibonacci Series
- Generate Armstrong Numbers
- C++ Find nCr and nPr
- C++ Get IP Address
- C++ Print Date/Time
- C++ Shutdown, Restart Computer
- C++ Programming Tutorial
- C++ Tutorial
- C++ Programming Test
- C++ Programming Test
- Give Online Test
- All Test List
C++ Program to Merge Two Arrays
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 Same Order as Provided by User
- Merge Two Arrays in Ascending Order
- Merge Two Arrays in Descending Order
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:
#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:
Now supply the input one by one. For example, if user enters:
- 5 as size for first array
- 1, 2, 3, 4, 5 as 5 elements for first array
- 5 as size for second array
- 6, 7, 8, 9, 10 as 5 elements for second array
then here is the output produced by above program after supplying these inputs and pressing ENTER
key:
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:
#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:
- 5 as size of first array
- 8 as size of second array
- 13, 1, 12, 3, 6 as 5 elements for first array
- 10, 11, 9, 2, 4, 4, 4, 7 as 8 elements for second array
After supplying these inputs, here is the output produced by above program:
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:
#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:
- 4 as size of first array
- 5 as size of second array
- 1, 2, 9, 2 as 4 elements for first array
- 1, 0, 8, 7, 3 as 5 elements for second array
The snapshot given below shows the sample output produced by above program, after supplying these inputs:
Same Program in Other Languages
« Previous Program Next Program »