Multi verses single dimension arrays...

Started by
4 comments, last by jacksaccountongamedev 20 years, 6 months ago
What is the point of a multi-dimensional array when the same thing can always be achieved using a single dimenional array? What I mean is this: int GameMap[100][100]; //can be replaced with int GameMap[100*100] Both achieve the same thing. As far as acessing goes: a=GameMap[x][y]; //as oposed to a=GameMap[y*100 + x]; Obviously the multi-dimensional array is easier to use when deling with many dimensions. And accessing the data in the single dimensional array will be slower because we have a multiplication and an adition as opposed just retrieving the data. Well I guess I have aswered my own question. Is there any other reasons that I am missing?
Advertisement
Syntax, mainly. It's the same physical representation in memory, yet you get the convenience of using multiple indices. So if you were working with a grid, you could use X and Y coordinates.

I can't think of any other reasons off the top of my head besides syntax and convenience.

[edited by - Zipster on October 10, 2003 11:30:56 PM]
Also, if you''re learning arrays, the multi-dimensional array is easier to learn. Myabe it''s just me, but when I first started the (y * 100 + x) messed me up on the linear arrays. Now that I know how to use them, that''s all I use.

-UltimaX-

"You wished for a white christmas... Now go shovel your wishes!"
Well, I think it is best to have a dimention in the array for each dimention you are going to represent, because that way, you know which objects, belong to what, and how to get them easier. Because you don''t have to create some sort of weird mapping scheme to get at the points you need.
Realloation of a multi-dimensional array is slower than reallocation of a single dimension array.
The main difference is the type (which sounds pretty obvious, but it''s actually very important). In terms of typesafety, a 100 element array of some type is completely different from a multidimensional array of the same type and they can not (directly) be used interchangeably. A pointer or reference to a multidiemnsional array is not equivalent to a pointer or reference to a single-dimensional array with the same number of elements. They conceptually represent different things even though implementation-wise they can be exactly the same. The best parallel that I can give are points and vectors (mathematical vectors). A point represents a location, while a vector represents magnitude with direction. Both are represented using the same components. This does not, however, mean they are the same concept, they just share similar low-level components. Much the same, multidimensional arrays, while internally are the same as single-dimensional arrays, differ in concept.

This topic is closed to new replies.

Advertisement