• Create Account

# Having a brain-fart about 2-D arrays

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

11 replies to this topic

### #1ysg  Members   -  Reputation: 192

Like
0Likes
Like

Posted 28 April 2013 - 02:34 PM

Hi all,

It seems that I'm -- embarrassingly -- having a hard-time with 2-D arrays and how to think of them conceptually. I'm currently working in C#, but ultimately, this can be applied to any language out there. If we have an array of foo[1][20], where would it matter that 'X' and 'Y' would be in relation to that array? How would that be handled (I realize that this would be very much compiler/run-time dependent)? Does it matter how I think about this conceptually or am I over-thinking this?

Thanks in advance if you can shed some light on this issue.

### #2Paradigm Shifter  Crossbones+   -  Reputation: 4748

Like
1Likes
Like

Posted 28 April 2013 - 02:41 PM

Think of the first number as the row and the second as the column. I don't know what you mean about X and Y in relation to the array, typically x is across though (X is a cross, geddit!!!) and y is up or down.

So a foo[1][20] array would be 1 row and 2 columns. May as well just be a 2D array in that case...

You can think of a 3D array as going into or out of the page. You can't visualise a 4D or higher dimensional array.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

### #3ysg  Members   -  Reputation: 192

Like
0Likes
Like

Posted 28 April 2013 - 02:54 PM

Think of the first number as the row and the second as the column. I don't know what you mean about X and Y in relation to the array, typically x is across though (X is a cross, geddit!!!) and y is up or down.

So a foo[1][20] array would be 1 row and 2 columns. May as well just be a 2D array in that case...

You can think of a 3D array as going into or out of the page. You can't visualise a 4D or higher dimensional array.

Ok, gotcha, much clearer now.

Thanks.

### #4radioteeth  Prime Members   -  Reputation: 674

Like
0Likes
Like

Posted 28 April 2013 - 02:54 PM

I think of it how it would be as a one-dimensional array, infact, I don't even use multidimensional arrays anymore (or arrays, if I can avoid it).. I just multiply all my dimensions and allocate one big chunk of memory, which I index as a linear array... In 2D I just imagine each row (eg: where X can lie inside of) being laid out end-to-end in one long line.. In 3D, I think of the same thing, except that once you reach the end of the first 'page' (0,0 to width,height) the beginning of the next Z 'page' is on the end of that.. So, 2D is a 'page' of 'lines', and 3D is just multiple pages. And, just like Paradigm Shifter pointer out, each [] bracket pair indicates another dimension, successively, so that in the case of XY, the first [] is X and the 2nd [] is Y.

### #5ysg  Members   -  Reputation: 192

Like
0Likes
Like

Posted 28 April 2013 - 02:56 PM

I think of it how it would be as a one-dimensional array, infact, I don't even use multidimensional arrays anymore (or arrays, if I can avoid it).. I just multiply all my dimensions and allocate one big chunk of memory, which I index as a linear array... In 2D I just imagine each row (eg: where X can lie inside of) being laid out end-to-end in one long line.. In 3D, I think of the same thing, except that once you reach the end of the first 'page' (0,0 to width,height) the beginning of the next Z 'page' is on the end of that.. So, 2D is a 'page' of 'lines', and 3D is just multiple pages. And, just like Paradigm Shifter pointer out, each [] bracket pair indicates another dimension, successively, so that in the case of XY, the first [] is X and the 2nd [] is Y.

Well... that's pretty much how it usually works in the memory of a computer . The multiple dimensions just make it easier for you. I realize you can do that, but it's just easier for me to do foo[20,20,20,40], as opposed to foo[320000] .

Edited by ysg, 28 April 2013 - 02:57 PM.

### #6Servant of the Lord  Crossbones+   -  Reputation: 14396

Like
0Likes
Like

Posted 28 April 2013 - 03:13 PM

I made some illustrations, and some poorly-worded descriptions in this post that might help.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                       [Need free cloud storage? I personally like DropBox]

### #7Kuxe  Members   -  Reputation: 208

Like
-1Likes
Like

Posted 28 April 2013 - 07:04 PM

Well... that's pretty much how it usually works in the memory of a computer . The multiple dimensions just make it easier for you. I realize you can do that, but it's just easier for me to do foo[20,20,20,40], as opposed to foo[320000] .

How about foo[20*20*20*40] ;)?

Also, easy to understand picture:

If you don't understand the stuff written here, please sharpen your C++ skills.

### #8kburkhart84  Members   -  Reputation: 1389

Like
0Likes
Like

Posted 28 April 2013 - 08:11 PM

I was going to mention that the code ends up making a single line of memory anyway(but someone already beat me to it).

These days, computers are fast enough that unless you are really needing some massive performance, as in are on a device other than even the lowest end PC, you are better off keeping code readable.  The big picture makes much more difference than a very small detail that makes little difference in speed.  So even if you got a slight speed gain by only using single index arrays, it wouldn't be worth losing the readability.  Also, you have to take into account the work to get that index, as multiplying has a slight cost too.  Much better to just do it the easier way and let the compiler speed it up.

About understanding and using arrays, do it as it makes sense to you.  If your game world is a 2d flat plane, then it makes good sense to use a 2d array to represent the tiles, unless you need a different structure, like maybe a 2d grid(as in the dynamic version).  As far as programming them goes, which index is 'x' and which one is 'y' doesn't really matter much, as long as all the code uses them the same way.  So like I said above, go with whatever makes sense and is easier to implement and understand later.

### #9Krohm  Crossbones+   -  Reputation: 2621

Like
0Likes
Like

Posted 29 April 2013 - 01:02 AM

I don't know about C#, but as far as I recall, Java multidimensional arrays are not contiguous in memory.

Note Servant of the Lord linked to a C++ specific topic.

Java arrays can be allocated on a per-line basis and don't even have to be the same size. At the same time, they cannot be linearized, unless they are born linear.

So, the concept is not to "be applied to any language out there". There are specific behaviors.

### #10mynameisnafe  Members   -  Reputation: 235

Like
0Likes
Like

Posted 29 April 2013 - 04:38 AM

Hi,

I asked a similar Question on here the other week regarding using a 1D array for bitmap storage, using x and y values to write to pixels.

Here's what I was taught to remember:

lets say C:

const int width= 10;

const int height = 10;

int * array_[ width - 1 * height - 1 ] ;

//write to the index value 1D array but take 2D array indices x and y

void modify( int x, int y, int index )

{

array_[ (x * width) + y ] = index ;

}

int main()

{

int x_ = 2;

int y_ = 3;

// Go up in the lift then walk along the corridor

int cur_index = ( x_ * width ) + y_ ;

//So you could loop through this 1D array as if it were a 2D array like so:

for( int x = 0; x < height; x += width )

for( int y = 0; y < width; y++ )

modify( x, y, (x * width) + y ) ;

return 0;

}

### #11warnexus  Prime Members   -  Reputation: 1181

Like
0Likes
Like

Posted 29 April 2013 - 10:01 AM

Think of the first number as the row and the second as the column. I don't know what you mean about X and Y in relation to the array, typically x is across though (X is a cross, geddit!!!) and y is up or down.

So a foo[1][20] array would be 1 row and 2 columns. May as well just be a 2D array in that case...

You can think of a 3D array as going into or out of the page. You can't visualise a 4D or higher dimensional array.

I'm pretty sure [1][20] is 1 row and 20 columns.

### #12Servant of the Lord  Crossbones+   -  Reputation: 14396

Like
0Likes
Like

Posted 29 April 2013 - 10:16 AM

Here's what I was taught to remember:

lets say C:

const int width= 10;
const int height = 10;

int * array_[ width - 1 * height - 1 ] ;

It's just (width * height).

An array that is 10 by 10 equals 100 cells. That's width * height (10 * 10 = 100).

If you subtract one from the width and height, that's 9 * 9 which is 81, which is an access violation for any row above 8.

Also, the order of mathematics (which C and C++ mostly follow) has multiplication resolved before subtraction.

width - 1 * height - 1

Actually becomes:
width + (-1 * height) - 1

Which is -1 (You can try it out here).

int cur_index = ( x_ * width ) + y_ ;

This should be (Y * Width) + X.

This works for any number of dimensions:
1D: Index = X
2D: Index = (Y * Width) + X
3D: Index = (Z * (Width * Height)) +  (Y * Width) + X
4D: Index = (T * (Width * Height * Depth)) + (Z * (Width * Height)) +  (Y * Width) + X

Edited by Servant of the Lord, 29 April 2013 - 11:14 AM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                       [Need free cloud storage? I personally like DropBox]

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS