In this example, you will learn how to merge two arrays into one in c++
C++ Code: C++ Program to Concatenate Two Arrays in C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
#include <iostream> using namespace std; int main() { int n,i; cout<<"size of the array"; cin>>n; int a[n],b[n],c[n*2]; cout<<"Array A "<<endl; for(i=0;i<n;i++) { cout<<"a[ "<<i<<" ] = "; cin>>a[i]; } cout<<"Array B "<<endl; for(i=0;i<n;i++) { cout<<"b[ "<<i<<" ] = "; cin>>b[i]; } //Concatenate A and B in C Lang for(i=0;i<n;i++) { c[i]=a[i]; c[n*2-(n-i)]=b[i]; } //Result of the merge cout<<"C = [ "; for(i=0;i<2*n;i++) cout<<c[i]<<" "; cout<<" ] "; return 0; } |
Output:
In this example,you have learned How to Add Two Arrays Together C++