Hi all,
I have to make a function which take as parameter two arrays and merge in one new.

For example
int array1[][] = {{1 , 2, 3, 4}, {2, 3, 4, 5}};
int array2[][] = {{5 , 6, 7, 8}, {6, 7, 8, 9}};

To take as result
arraynew[][] = {{1 , 2, 3, 4}, {2, 3, 4, 5},{5 , 6, 7, 8}, {6, 7, 8, 9};

Array1 and array2 have 4 columns but the can have more than 2 rows.

Could you help me ???

2
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by ndeniche

assuming you got column size compatibility between arrays, you must create an array with `x` quantity of rows, `x` being the sum of array1's rows and array's 2 rows (I assume you know these sizes, since you had to declare the arrays sizes). After that, you create two nester for loops to go through both arrays one after another. This way:

``````int sizex = 4, sizey1 = 2; sizey2 = 3;
int array1[sizex][sizey1]={{1,2,3,4},{2,3,4,5}};
int array2[sizex][sizey2]={{3,4,5,6},{4,5,6,7},{5,6,7,8}};
int array3[sizex][sizey1+sizey2];

for (int x = 0 ; x < sizex ; x+=1)
{
for (int y = 0 ; y < sizey1 ; y+=1)
{
array3[x][y] = array1[x][y];
}
}
for (int x = 0 ; x < sizex ; x+=1)
{
for (int y = sizey1 ; y < sizey2 ; y+=1)
{
array3[x][y] = array1[x][y];
}
}``````

this way, `array3` would be:

{1,2,3,4}
{2,3,4,5}
{3,4,5,6}
{4,5,6,7}
{5,6,7,8}

Edited by ndeniche: Bilingual coding :P