As the title indicates, I want to concatenate two arrays into one larger array. for example:
array1= 1 2 3
4 5 6
7 8 9
array1= 10 20 30
40 50 60
70 80 90
array3= 1 2 3 10 20 30
4 5 6 40 50 60
7 8 9 70 80 90
So, I wrote this code:
#include <iostream>
using namespace std;
int main()
{
int array1[3][3],array2[3][3],array3[3][6];
int i,j;
//Matrix 1 input
cout<<"Enter matrix 1\n";
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
cin>>array1[i][j];
array3[i][j]=array1[i][j]; //Assigning array1 values to array3
}
}
//array2 input
cout<<"Enter matrix 2\n";
for (i=0;i<3;i++)
{
for (j=3;j<6;j++)
{
cin>>array2[i][j];
array3[i][j]=array2[i][j]; //Assigning array2 values to array3
}
}
//array3 output
cout<<"New matrix is\n";
for (i=0;i<3;i++)
{
for (j=0;j<6;j++)
{
cout<<array3[i][j]<<"\t";
}
cout<<"\n";
}
}
But when I execute it, I ended up with last row of array2 being the (2,3), (2,4) and (2,5) elements (which is right), but also being the (0,1), (0,2) and (0,3) elemnts (which should be [1 2 3]).
array3= 70 80 90 10 20 30
4 5 6 40 50 60
7 8 9 70 80 90
So, what's happening here?
EDIT: I did the following:
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
cin>>array2[i][j];
array3[i][j+3]=array2[i][j]; //Assigning array2 values to array3 and adding 3 to j
}
And it went ok. Is the method I used "professional"? }
array2 inputpart,jis[3,6)whilearray2isarray2[3][3]