Archived

This topic is now archived and is closed to further replies.

Laz

Stupid question about 2-Dimensional Arrays

Recommended Posts

Okay I feel like an idiot for asking this, but it''s been 4 years since i''ve done anything with a 2 dimensional array. So here we go: I want a grid that is 7 across and 5 down, how would I make that into a 2D array? int array[7][5] or int array[5][7]? I''m confused :/ Sorry for the stupid question. -Laz

Share this post


Link to post
Share on other sites
*shrugs*

It''s all data ^_^

If you access your array using ''list[x][y]'', then ''int list[7][5]''.

If you access your array using ''list[y][x]'', then ''int list[5][7]''.

Share this post


Link to post
Share on other sites
It doesnt make the slightest difference, what makes it 7 across by 5 down is how you process it.

Personally I''d do it as [7][5] (width, height).

NightWraith

Share this post


Link to post
Share on other sites
It really don''t matter as long as you stick to your own definition of which is which.

n-dimensionals are slow (2dim is about 3 times slower than a 1dim used as a 2dim...did a small test-app in cs class once..).

Share this post


Link to post
Share on other sites
Well I just thought about it and I can eliminate the confusion and probably free up some memory by using a linear array. It''s a custom data type and in my structure I have xPos and yPos, so I can just go through the linear array displaying each element at it''s xPos and yPos ... I guess I was thinking of 2D arrays because it will help visualize how the grid is supposed to look... if that makes sense...

Share this post


Link to post
Share on other sites
Its basically arbitrary. As long as you stay consistant, either way works. The only thing I''d observe is how many pointers you''d have. 7*5 = 35 cells. However, C arranges multidimensional arrays as arrays of pointers to other arrays. So, you''d have 5 pointers if it was array[5][7] and 7 pointers if it was array[7][5]. So, you in fact have 40 and 42 memory cells in use in each situation respectively. Though, with computers today pushing the limits of a 32bit address space, I don''t think you should worry about it. In fact, if you haven''t learn pointers, or, just aren''t comfortable with them yet, ignore my post.

-> Will Bubel
-> Machine wash cold, tumble dry.

Share this post


Link to post
Share on other sites
It does make some slight difference, at least with pointer calls:

  
int a[5][6];
int *p_a = &a[2][3];
*(p_a + 1) = 5; //a[3][3] or a[2][4]???


I''m not totally sure about this, but I think it would be a[ 2 ][ 4 ]... I''d have to check, though.

Cédric

Share this post


Link to post
Share on other sites