C++ Program to merge two arrays [DEVCPP/GCC]


//Program to merge two arrays (in an efficient way)

# include<iostream>

using namespace std;

int main()
{
int a[4],b[4],c[8],i,j;

cout<<"ENTER THE ELEMENTS OF ARRAY1:\n";
 
          for(i=0 ; i<4 ; i++)             //Loop to input array a
{
cout<<"a["<<i<<"]=";
cin>>a[i];
}
 
cout<<"\nENTER THE ELEMENTS OF ARRAY2:\n";

for(i=0 ; i<4 ; i++)              //Loop to input array b
{
cout<<"b["<<i<<"]=";
cin>>b[i];
}

           for(i=0 , j=0 ; i<4 ; i++ , j=j+2)  //Loop for merging
{
c[j]=a[i];
c[j+1]=b[i];
}

cout<<"\nMERGED ARRAY:\n";

for(i=0 ; i<8 ; i++)
{
cout<<"c["<<i<<"]="<<c[i]<<endl;
}

return 0;
}

C++ Program to merge two arrays with output

Share this

Related Posts

FIND US ON FACEBOOK!