- C++ Programming Examples
- C++ Programming Examples
- C++: Hello World
- C++: Get Input
- C++: Print Integer
- C++: Add two numbers
- C++: Add, Sub, Multiply, Div
- C++: Add Digits
- C++: Find Average and Percentage
- C++: Find Arithmetic Mean
- C++: Sum of n Natural Numbers
- C++: Sum of n Numbers
- C++: Square's Area and Perimeter
- C++: Rectangle's Area and Perimeter
- C++: Triangle's Area and Perimeter
- C++: Area and Circumference
- 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 a Number
- C++: Find Factors of a Number
- C++: Find HCF and LCM
- C++: Create a Calculator
- C++: Count Digits in a Number
- C++: First and Last Digit Sum
- C++: Product of Number Digits
- 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
- C++: Check Reverse equals Original
- C++: Check Perfect Number
- C++: Check Palindrome or Not
- C++: Check Armstrong or Not
- C++: Divisibility Test
- C++: Find Labor Wage
- 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
- C++: Largest Element in an Array
- C++: Smallest Element in an Array
- C++: Find Second Largest Element
- C++: Find Second Smallest Element
- C++: Sum of All Elements
- C++: Multiply All Elements
- C++: Element in Even Position
- C++: Element in Odd Position
- C++: Print Even Numbers in Array
- C++: Print Odd Numbers in Array
- C++: Count Even or Odd Numbers
- C++: Sum of Even or Odd Numbers
- C++: Count Positive, Negative, Zero
- C++: Reverse an Array
- C++: Insert an Element
- C++: Delete an Element
- 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 String Length
- C++: Compare Two Strings
- C++: Copy String
- C++: String Concatenation
- C++: Reverse a String
- C++: Delete Vowels from a String
- C++: Delete a Word from a String
- C++: Count Characters in a String
- C++: Count Words in a String
- C++: Frequency of Words
- C++: Remove Spaces from Strings
- C++: Sort a String
- C++: Uppercase to Lowercase
- C++: Lowercase to Uppercase
- C++: Swap Two Strings
- C++: Check the Anagram or Not
- C++: Capitalize All Words in a String
- C++: Get Numbers from a String
- C++ File Programs
- C++: Read a File
- C++: Write Content to a File
- C++: Append Data to a File
- C++: Read and Display File
- C++: Copy a File
- C++: Merge Two Files
- Count Characters in a File
- C++: Capitalize Every Word
- C++: List Files in Directory
- C++: Delete a File
- C++: Encrypt and Decrypt a File
- C++ Misc Programs
- C++: Print ASCII Value
- C++: Add Binary Numbers
- C++: Generate Random Numbers
- C++: Print a Smiling Face
- C++: Days into Years and Months
- C++: Add Two Numbers using Pointer
- C++: Print Fibonacci Series
- C++: Generate Armstrong Numbers
- C++: Find nCr and nPr
- C++: Get IP Address
- C++: Print Date and Time
- C++: Shutdown and Restart Computer
- C++ Programming Tutorial
- C++ Tutorial
C++ Program to Merge Two Arrays
In this article, you will learn and get code to merge two arrays entered by the user at run-time in the C++ language. Here is the list of programs on the merging of two arrays available in this article:
- Merge two arrays in the same order that the user specifies
- 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 the user to enter the sizes and elements for both arrays. Then merge these two given arrays into a third array, as shown in the program given below:
Note: At the time of receiving the array's elements, we applied the merge operation. That is, the elements received from the user are added one by one to a third array.
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 built and runs under the Code::Blocks IDE. Here is its sample run:
Now supply the inputs one by one. For example, if the user enters:
- 5 as the size for the first array.
- The first array's elements are 1, 2, 3, 4, and 5.
- 5 as the second array's size.
- 6, 7, 8, 9, 10 as 5 elements for the second array.
then here is the output produced by the above program after supplying these inputs and pressing the ENTER key:
You can also sort the two arrays, then merge. Alternatively, sort after merging. Here is the list of sorting techniques you can go with:
Merge Two Arrays in Ascending Order
This program merges two arrays entered by the user at run-time in ascending order. That is, the two arrays get 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 the following user inputs:
- 5 as the size of the first array
- 8 as the size of the second array
- 13, 1, 12, 3, 6, as 5 elements for the first array
- The second array has 8 elements: 10, 11, 9, 2, 4, 4, 4, 7.
After supplying these inputs, here is the output produced by the above program:
Merge two arrays in descending order
To merge two arrays in descending order, change the following code (from the previous program):
if(arrMerge[i]>arrMerge[i+1])
with
if(arrMerge[i]<arrMerge[i+1])
It's worth noting that changing the greater-than and less-than symbols changes the entire program.
Everything else will remain the same.That is, the code that sorted merged arrays in ascending order in the previous program has changed to sort the same arrays in descending order.
But still, if you want the complete program on the 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 the following user inputs:
- 4 as the size of the first array
- The size of the second array is set to 5.
- 1, 2, 9, 2 as 4 elements for the first array
- 1, 0, 8, 7, 3 as 5 elements for the second array
The snapshot given below shows the sample output produced by the above program after supplying these inputs:
The same program in different languages
« Previous Program Next Program »