0

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 ???
Please

2
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by Nichito
-2

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 Nichito: Bilingual coding :P

Votes + Comments
Giving away homework still? You really know better.
-2

remember marking solved threads as solved ;)

Votes + Comments
why? you keeping score? what makes youi think your sloppy snippet is worth a "solved" anyhow?
Maybe you should share your bath with a toaster, then you won't have to beg for solved threads anymore.
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.