Merge Two Arrays
Merge Two Arrays in C++ (5 Programs)
BeginnerTopic: Array Operations Programs
C++ Merge Two Arrays Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
using namespace std;
int main() {
int arr1[] = {1, 2, 3};
int arr2[] = {4, 5, 6};
int n1 = 3, n2 = 3;
int merged[n1 + n2];
// Copy first array
for (int i = 0; i < n1; i++) {
merged[i] = arr1[i];
}
// Copy second array
for (int i = 0; i < n2; i++) {
merged[n1 + i] = arr2[i];
}
cout << "Merged array: ";
for (int i = 0; i < n1 + n2; i++) {
cout << merged[i] << " ";
}
cout << endl;
return 0;
}Output
Merged array: 1 2 3 4 5 6
Understanding Merge Two Arrays
This program demonstrates 5 different methods to merge two arrays: simple concatenation, using vectors, using copy() algorithm, using memcpy(), and using dynamic arrays.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.