george_t
george_t

Reputation: 188

Merging two arrays in a C++ program

I want to merge two arrays into one in a C++ program. For example:

int A[150],B[150];
int C[150][2];

And I want to have them as column vectors in C. For example in MATLAB I could use C=[A;B]. What is the easiest way?

Upvotes: 0

Views: 11896

Answers (2)

arun
arun

Reputation: 26

Try this.you can feel better comparing to another code.

using namespace std;

int main()
{
int a[5]={3,2,1,4,5};
int  b[5]={9,8,6,7,0};
int c[10];    
for(int i=0;i<=4;i++)
{
    cout<<"\n"<<a[i];

}

for(int i=0;i<=4;i++)
{
    cout<<"\n"<<b[i];
}
for(int i=0;i<=4;i++)
{
    c[i]=a[i];
}
for(int i=0,k=5;k<=10&&i<5;i++,k++)
{
    c[k]=b[i];
}
cout<<"merging";
for(int i=0;i<=9;i++)
{
    cout<<"\n";
cout<<"\t"<<c[i];
}}

Upvotes: 0

LibertyPaul
LibertyPaul

Reputation: 1167

for(int i = 0; i < 150; ++i){
    c[i][0] = a[i];
    c[i][1] = b[i];
}

Upvotes: 3

Related Questions