hey guys i want to concatenate multiple 2d arrays in c this what happens when i just do two, but if i do more its sume together.it seems like every cycle it bushes one up:
array 1
222222222222222222222
000000000001000000000
000000000001000000000
000000000001000000000
000000000001000000000
111111111111111111111
array 2
222222222222222222222
000000000001000000000
000000000001000000000
000000000000000000000
000000000001000000000
111111111111111111111
what i want
222222222222222222222222222222222222222222
000000000001000000000000000000001000000000
000000000001000000000000000000001000000000
000000000001000000000000000000000000000000
000000000001000000000000000000001000000000
111111111111111111111111111111111111111111
what i get
222222222222222222222000000000001000000000
000000000001000000000000000000001000000000
000000000001000000000000000000000000000000
000000000001000000000000000000001000000000
000000000001000000000000111111111111111111
111111111111111111111111111111111111111111
function to concatenate
#define map_height 6
#define map_length 21
#define map_stage_length 5
char ** concatenate(){
char **array=create2DCharArray(map_height,map_length*map_stage_length);
char **mapsarray[5]={create2DCharArray(map_height,map_length),
create2DCharArray(map_height,map_length),
create2DCharArray(map_height,map_length),
create2DCharArray(map_height,map_length),
create2DCharArray(map_height,map_length)};
int i=0,j=0,n=0;
mapsarray[0]=load_map("./resources/maps/map1.map");
mapsarray[1]=load_map("./resources/maps/map2.map");
mapsarray[2]=load_map("./resources/maps/map2.map");
mapsarray[3]=load_map("./resources/maps/map2.map");
mapsarray[4]=load_map("./resources/maps/map2.map");
for (i = 0; i < map_height; i++)
{
for (n = 0; n < map_stage_length; n++)
{
for (j = 0; j < map_length; j++)
{
array[i][(n*map_length)+j]=mapsarray[n][i][j];
}
}
}
return array;
}
du you have any ideas why it pushes up?
Do try this loop.
for(i=0; i<map_stage_length; i++){ // for each 2d Array, ith array
for(r=0; r<map_height; r++){ // for each col
for(c=0; c<map_length; c++) // for each row
array[r][ (i*map_length) + c ] = mapsarray[i][r][c] ;
}
}
use good variable names instead i,j ,k
Here's a hint to make debugging this easier. Make the outermost loop iterate through 'n'. Each mapsarray[n]
is an input array. Break out the rest of your code into a function that simply writes the data from the input array into the correct location in the output array. What you ultimately want is something like this:
for (i = 0; i < num_maps; ++i)
{
concatenate_array(array, mapsarray[n], n);
}
With that architecture, you can execute the concatenate_array
function on each input individually and observe where the data gets written. This should make it more obvious where your array index arithmetic or loop boundary conditions need to be adjusted.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.