4

I became confused when GCC allowed me to do this:

int t[10][10][10][10][10];

I realize int i[N][N] is an NxN matrix where the first N means the row, and the second means the column. Also, the third N in int i[N][N][N] means depth, giving us a 3d dimensional array.

I do not understand what int i[N][N][N][N] and beyond mean.

The fourth dimension is time, but that does not apply here.

So, could this mean that when I get to the third one, I could let the logic go away?

andand
  • 17,134
  • 11
  • 53
  • 79
Patrick Bassut
  • 3,310
  • 5
  • 31
  • 54
  • 2
    I'm not really sure what your question is. You use a multidimensional array if you have a multidimensional dataset to represent... – Oliver Charlesworth Jul 20 '12 at 16:54
  • 1
    Queue Morgan Freeman (http://en.wikipedia.org/wiki/Through_the_Wormhole) – Inisheer Jul 20 '12 at 16:55
  • 7
    For me, I imagine the spacial dimensions starting over. 3 dimensions make a cube, 4 dimensions are a line of cubes, 5 is a table of cubes, 6 is a cube of cubes, etc. – Corey Ogburn Jul 20 '12 at 16:55
  • 1
    The number of dimensions in our world go far beyond what we can perceive. – chris Jul 20 '12 at 16:55
  • 3
    Things in mathematics do not necessarily align to things that can be visualized in our world easily. – Don Roby Jul 20 '12 at 16:56
  • 8
    There's no reason the dimensions in your array have to correspond to anything to do with the physical world. – Benjamin Lindley Jul 20 '12 at 16:56
  • 1
    On the other hand, it could be time I suppose. In a computer simulation the first 3 dimensions could be the model and the 4th dimension is that model at different times. Of course that makes the 5th dimension confusing to think about. I think this is how String theory started. "If C++ can do it... why can't the universe?" – Corey Ogburn Jul 20 '12 at 16:57
  • How is this a progamming question? – anio Jul 20 '12 at 16:58
  • @anio "Applications of data structures with excessive dimensions?" Just a guess. – Corey Ogburn Jul 20 '12 at 16:59
  • By the way, why does time have to be the 4th dimension? Why can't it be the 3rd, 2nd or 1st? Or 5th or greater if there are that many. – Benjamin Lindley Jul 20 '12 at 16:59
  • Not only is this not programming, but although the 4th dimension can be thought of as time in some ways, in geometry, you can have as many dimensions as you want. The fact that they are imposible to visualize based on human existence is irrelevant. – Linuxios Jul 20 '12 at 17:00
  • We should move to chat. This is becoming excessive and I still want to talk about it more, ha ha. http://chat.stackoverflow.com/rooms/14191/n-dimensional-c-array-hows-that-possible – Corey Ogburn Jul 20 '12 at 17:01
  • @BenjaminLindley - Just a guess, but I would think the first 3 were annotated / discovered before the inclusion of time as a dimension. Just as if we decide there is another dimension, it would likely be the 5th (or whatever we're up to now :) ). – Inisheer Jul 20 '12 at 17:01
  • @PatrickBassut I think you would get a fair chance at having this question reopened if you edit its title to "how can I visualize an n-dimensional array", and clean up certain verbiage that others may find offensive. I am nominating this for a re-open, I think it's a good question that could benefit from a restatement. – Sergey Kalinichenko Jul 20 '12 at 17:20
  • 1
    Of course this is a programming question. Datastructures are part of programming. Asking why a datastructure would be useful or how you would use it seems like a reasonable question. – Brian Neal Jul 20 '12 at 18:00

6 Answers6

27

I find a library analogy very useful to visualizing arrays of multiple dimensions:

  • 8-dimensional array is a library
  • 7-dimensional array is a floor in a library
  • 6-dimensional array is a room on a floor in a library
  • 5-dimensional array is a bookcase in a room on a floor in a library
  • 4-dimensional array is a shelf in a bookcase in a room on a floor in a library
  • 3-dimensional array is a book on a shelf in a bookcase in a room on a floor in a library
  • 2-dimensional array is a page in a book on a shelf in a bookcase in a room on a floor in a library
  • 1-dimensional array is a line on a page in a book on a shelf in a bookcase in a room on a floor in a library
  • 0-dimensional array is a character in a line on a page in a book on a shelf in a bookcase in a room on a floor in a library
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • +1 for a good analogy. Why don't you introduce the nineth dimension for time spent in the library? ;-) – stefan Jul 20 '12 at 16:59
  • I wish I could +1 a few more times. – Corey Ogburn Jul 20 '12 at 17:00
  • 1
    Nesting like this isn't really a great example of dimensions IMO, because dimensions are usually independant. It doesn't make sense to refer to the same character of the same line of the same book on the same sheft of the same bookcase on the same floor of a _different_ library. – Mooing Duck Jul 02 '13 at 00:35
  • I believe [this](http://stackoverflow.com/questions/5580761/why-use-double-pointer-or-why-use-pointers-to-pointers) is the original answer – alex Mar 13 '17 at 10:18
  • @alex No, you are wrong. This answer is 100% original, not based on anybody else's work. The analogy has been suggested to me by my CS teacher back in the eighties. – Sergey Kalinichenko Mar 13 '17 at 10:24
  • @dasblinkenlight Oh, my bad! It indeed makes sense to be way older than a couple of years. – alex Mar 13 '17 at 10:26
10

In the world of mathematics, the number of dimensions doesn't matter. It just eventually gets to a point at which one can no longer visualize it.

jrad
  • 3,172
  • 3
  • 22
  • 24
8

The dimensions are just whatever you want to make of them. For example, depth and time only make sense when you're dealing with those concepts.

It doesn't have to be about space and time. In fact, the C++ standard calls them extents.

Let's say you have ten different cheeses, and you want to rate the likelihood of someone preferring them in some particular order. You could store that in your int t[10][10][10][10][10];, with the extents meaning, respectively: favourite cheese, second favourite cheese, third favourite cheese, fourth favourite cheese, fifth favourite cheese, and least favourite cheese. The likelihood of someone preferring cheeses in the order 5-4-6-3-2-1 would be expressed as t[5][4][6][3][2][1].

The point is, the language attaches no domain semantics to the extents. It's up to you to do so.

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
3

N-dimensional arrays aren't just a C++ thing. It appears all over in math, physics, various other sciences, etc.

Here's an example: say you want to index data by position (x,y,z), time, and "which user generated the data." For a data point collected at x1, y1, z1, time1, and generated by user1, you'd save it in dataArray[x1][y1][z1][time1][user1] = myNewData.

solvingPuzzles
  • 8,541
  • 16
  • 69
  • 112
1

In programming, don't think of multidimensional arrays in terms of traditional geometry unless you are directly trying to represent the world. It is better to think of each successive "dimension" as another array containing arrays. There are several use-cases where this may appear. However, if you are using more than three dimensions I would no longer think of it as arrays or even "arrays of arrays", I prefer trees are they are closer to how you would program something that needs more than 3 levels deep.

One example is a Tree, where you have a root node, which has nodes which also have nodes. If you wanted to sort something a tree is a wonderful tool. Let's say you wanted to sort a bunch of numbers that came in randomly. You would make the first number that came in the root. If the first number was a 5, and the next number were a 7, then you would put the 7 to "the right" of the root node 5. And if you got a 3 then a 4, you would insert the 3 to the "left" of 5, and then the 4 to the "right" of the 3. If you traverse this tree in-order (by always going left down the tree, only going back up when there are no new nodes and then going right), you will end up with a sorted list: 3, 4, 5, 7.

       5
    /    \
   3      7
     \
      4

Here, you can see the tree structure. If you were doing this in C, you would use structs, which would looks like this (I am using pseudo-code):

struct Node{
     int val;
     Node left;
     Node right;
}

There are lots of materials on Binary Trees (what I have been explaining), but primarily I wanted you to move away from the concept of arrays being "like dimensions in space", and much more of just a data-structure that can store elements. Sometimes a binary-tree or other data-structure is too complex, and a 5 or more dimensional array may be more convenient for storing data. I can't quite think of an example right now, but they have been used before.

kurtzbot
  • 512
  • 6
  • 19
  • 1
    I didn't see dasblinkenlight's answer while I was typing this, but that is an excellent example of something that can be represented by a nine-dimensional array. Now, I would not recommend that kind of data-structure for storing something like that, but it does help to give an example use-case where multi-dimensional arrays may be appropriate. – kurtzbot Jul 20 '12 at 17:21
0

As physical 3 dimensional beings, we cannot "visualize" what a 4th, 5th, 6th (or above) physical dimensions represent.

A 4th dimension would extend our perception into a 4th direction which would be orthogonal to the height, width and depth directions we naturally perceive.
Yes - geometry gone weird !!

In order to give us a feeling for this idea, in this video Carl Sagan imagines what it would feel like to a perfectly flat 2d creature (a little square), living in a 2d world, to encounter a mysterious 3d creature.
This 3d creature (suspiciously similar to an apple) exists mostly in this mysterious 3rd dimension that the little square cannot "see". It only perceives the points of the apple that intersect with its 2d plane world, i.e. its projection...

The video looks old-fashioned by today's standards, but in terms of physics/geometry is still the best explanation I've seen out there.

Daniel
  • 190
  • 1
  • 5