# get nxn size blocks from 2d array?

This is a discussion on get nxn size blocks from 2d array? within the C Programming forums, part of the General Programming Boards category; Hi all I am so confuse about getting nxn blocks from 2d array, for an instance: an 2d array of ...

1. ## get nxn size blocks from 2d array?

Hi all I am so confuse about getting nxn blocks from 2d array, for an instance:

an 2d array of size is 512*512, and has numbers filled already and its like:

array1[512][512] is

Code:
```0,    1,    2, .........511
512,  513'  514.......1023
1024, 1025' 1026......1335
:```
and I'd like to copy 4x4 size block from array to
array2[16384][4][4]
(512*512/16=16384 is number of blocks)

so 1st block of array2 will be

Code:
```0,    1,    2,    3
512,  513'  514,  515
1024, 1025' 1026, 1027
1336, 1337, 1338,  1339```
my code:
Code:
```int k=0;
for(i=0; i<512; i++)
for(j=0; j<512; j++)
for(m=0; m<4; m++)
for(n=0; n<4; n++){
array2[k][m][n]=array1[i%4][j%4];
k++;
}```
but i got bus error and so confuse
can anyone help me out please?

2. From my calculations, you are incrementing k way more than 31 times, but the first element of array2 only goes upto 31.

3. sorry k should up to 16384 b/c it is numbers of block with 4x4 size

4. But you have four nested for loops, with 512,512,4,4 iterations respectively, so you are incremending k 4*4*512*512 times = 4194304. So once you hit 16384, you're going out of bounds. re-think the logic of how you're doing the copy.

5. Well that 3D array is ugly.

Try doing this in 1D. First you will have 2 offsets. One offset indexes into the source array and one indexes into the destination array.

Code:
```...
//start_row, end_row,start_column,end_column are parameters to this function
int source_offset=((start_row)*source_width)+start_column;
int source_start_offset=source_offset;
int source_height_counter=0;
int source_width_counter=0;
int dest_offset=0;
int source_width=end_column-start_column;
int source_height=end_row-start_row;

//Create destination array
dest = new int[source_width*source_height];
if (!dest) return;

//Clear destination array
memset(dest,0,source_width*source_height*sizeof(int));

//Set loop flag
bool bExitLoop=false;

do
{
dest[dest_offset]=source[source_offset];
source_offset++;
dest_offset++;
source_width_counter++;
if (source_width_counter>source_width)
{
source_start_offset+=source_width;
source_offset=source_start_offset;
source_height_counter++;
source_width_counter=0;
}
if (source_height_counter==source_height && source_width_counter==source_width)
{
bExitLoop=true;
}
} while (bExitLoop==false);```
Assuming my variables and code are correct, this should copy a NxN block from source to dest providing the following are true:
• start_row, end_row, start_column, and end_column are all within the bounds of source array - in other words the starting row and column and the ending row and column of the requested source block do in fact lie within the boundaries of the array. No bounds checking is done in this code.
• There is enough memory for the dest array
• end_row is larger than start_row
• end_column is larger than start_column

I leave it as an exercise for you to put this into a function and return a pointer to dest array. This is not pure C - use malloc instead of new. Sorry, I'm so used to C++ I mix C and C++ all the time.

In C/C++ this would be a good function to put into a template class because you could copy a block of NxN anything into dest. Dest would have the same data type as source and could easily be coded as a template class.