0

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
george_t
  • 188
  • 1
  • 3
  • 19
  • It depends on if you want to copy them into the third array or if you want to have the third array hold references to the first two. – callyalater Mar 24 '16 at 13:48
  • doing a basic loop which would took 2 sec to code ? and C++ is an Object Oriented language, so use object, for array you got the templated array in the standard library – Guiroux Mar 24 '16 at 13:48
  • the easiest way is this `std::vector a; std::vector b; std::vector > c; c.push_back(a); c.push_back(b);` – 463035818_is_not_an_ai Mar 24 '16 at 13:49
  • @tobi303 but this will hold the whole array on index 0, the whole array on index 1. IS this what he is asking for? maybe every element of both arrays on an index then need a for loop and push_back(s) for every array – Khalil Khalaf Mar 24 '16 at 13:50
  • Possible duplicate of [Is there a function to copy an array in C/C++?](http://stackoverflow.com/questions/16137953/is-there-a-function-to-copy-an-array-in-c-c) – hlscalon Mar 24 '16 at 13:52
  • @FirstStep i dont really understand, do you mean that he is asking for a `[150][2]` sized array, while I suggest dimensions `[2][150]` ? – 463035818_is_not_an_ai Mar 24 '16 at 13:54

2 Answers2

3
for(int i = 0; i < 150; ++i){
    c[i][0] = a[i];
    c[i][1] = b[i];
}
LibertyPaul
  • 1,139
  • 8
  • 26
0

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];
}}
arun
  • 26
  • 5