Badges: 12
Report Thread starter 11 months ago

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?

Badges: 19
Report 11 months ago
(Original post by Yodalam)

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?

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:
meow = myArray[a][b][c][d][e][f][g][h][i][j];
Your program would probably be drowning in for-loops whenever you're doing anything with the data in that array, but the worst part would be that someone trying to understand what each 'dimension' actually means would probably tie themselves up in knots trying to get their head around it - that probably would include you trying to write that code too.

Whatever problem you'd be trying to solve with an N-dimensional array, it's always worth asking the question about what might be a more idiomatic solution (i.e. thinking of how to write code whose intent and purpose is more immediately obvious). Usually the simplest solution you can think of will turn out to be the best one, because if something doesn't work properly, or it needs to change later, then simplicity tends to be easier to work with.

There's a famous quote which starts "Any fool can make things bigger and more complex...". - it's true in programming too! Except that complexity in programming usually refers to the cognitive overhead of reading a bit of code - how much information someone needs to store in their head to understand what's going on.

A lot of complexity written in a programming language usually leads to cause/effect like a game of jenga - i.e. you have some code which is too complex to fit into your head all at once, so when you change one tiny bit, it turns out that you've made some mistakes and the rest of the program collapses in a heap, then eventually it starts to look easier just to throw it away and start again than to spend hours/days trying to fix one tiny problem or add one small new feature. Quite often when someone talks about 'good' and 'bad' in a programming sense, they're talking about the habits and techniques which avoid code having a logical flow which resembles spaghetti, or which looks like a 'big ball of mud' where everything appears to be mashed together.

Quick Reply

Attached files
Write a reply...
new posts
to top
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.


University open days

  • University of Stirling
    Undergraduate Open Day Undergraduate
    Thu, 26 Sep '19
  • Heriot-Watt University
    Undergraduate Open Day - Scottish Borders Campus Undergraduate
    Fri, 27 Sep '19
  • Royal Holloway, University of London
    Undergraduate open day Undergraduate
    Sat, 28 Sep '19

Are you attending a Global Climate Strike?

Yes, I'm striking (21)
No, but I wanted to/I support the cause (203)
No (124)

Watched Threads

View All