The Student Room Group

A level computer science

HI TSR,

I was just wondering whether it is possible to have an n-dimensional array holding a set of elements of the same type. And an example of where it might be useful to use a multi dimensional array?

Thanks
Original post by Yodalam
HI TSR,

I was just wondering whether it is possible to have an n-dimensional array holding a set of elements of the same type. And an example of where it might be useful to use a multi dimensional array?

Thanks

Yes, you can have as many dimensions in an array as you like. Is it useful? Occasionally - for example, a Matrix or Chess board could be implemented to use a 2D array. A good use-case for it would usually need to be something that's commonly represented using 2 dimensions in the real world.

The term '2D array' is slightly misleading too - it's not really a 2D array. it's a 1D array which contains other 1D arrays. And a 3D array is a 1D array which contains some 2D arrays. It's a bit like a set of multi-dimensional russian dolls.

More generally a better question would be 'Is it a good idea'? I'd lean towards "Probably not, unless you've really thought about it and have reasons not to choose any of the alternatives". The problem with multi-dimensional arrays is that they're not very intuitive for humans to reason over.

Anything beyond a 2-dimensional array is usually a 'red flag' that your code is probably getting too complex for someone to be able to easily understand what's going on, and it indicates there's likely a more human-friendly way to design your program so that the intent of the code is more obvious, and where the code can be related back to the problem it's actually trying to solve; this is usually where a bit of Object-oriented programming/design can be useful to learn because that's a topic which all boils down to reducing complexity and increasing human-readability..

To put an extreme example, you could have a 10-dimensional array, but that means something in your code trying to access an element in that 10-dimensional array is going to look like this:
(edited 5 years ago)

Quick Reply

Latest

Trending

Trending