N-dimensional array of C ++. How is this possible?

I was embarrassed when the GCC allowed me to do this:

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

I understand that int i[N][N] is an NxN matrix, where the first N stands for row and the second stands for column. In addition, the third N in int i[N][N][N] means depth, providing us with a three-dimensional dimensional array.

I do not understand that int i[N][N][N][N] and is out of the middle.

The fourth dimension is time, but it is not applicable here.

So, can this mean that when I get to the third, I can let the logic go?

+4
source share
6 answers

I find the library analogy very useful for visualizing arrays of several dimensions:

  • 8-dimensional array is a library
  • The 7-dimensional array represents the library floor
  • A 6-dimensional array is a room on the floor in the library.
  • A 5-dimensional array is a bookcase in a room on the floor in the library.
  • A 4-dimensional array is a shelf in a bookcase in a room on the floor in the library.
  • A 3-dimensional array is a book on a shelf in a bookcase in a room on the floor in the library.
  • A 2-dimensional array is a page in a book on a shelf in a bookcase in a room on the floor in the library.
  • A 1-dimensional array is a line on a page in a book on a shelf in a bookcase in a room on the floor in the library
  • A 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 the floor in the library.
+26
source

In the world of mathematics, the number of dimensions does not matter. He just gets to the point where he can no longer visualize it.

+10
source

Dimensions are what you want to make from them. For example, depth and time only make sense when you are dealing with these concepts.

It should not 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 evaluate the likelihood that someone will prefer them in a certain order. You can save this in your int t[10][10][10][10][10]; meaning the extent: favorite cheese, second favorite cheese, third favorite cheese, fourth favorite cheese, fifth favorite cheese and least favorite cheese. The likelihood that someone prefers cheeses in the order of 5-4-6-3-2-1 will be expressed as t[5][4][6][3][2][1] .

The fact is that the language does not attach domain semantics to extents. It is for you to do this.

+8
source

N-dimensional arrays are not just C ++. He appears everywhere in mathematics, physics, various other sciences, etc.

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

+3
source

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

One example is a tree where you have a root node that has nodes, which also have nodes. If you want to pick something, then a tree is a great tool. Let's say you wanted to sort a bunch of numbers that came in random order. You would make the first number that appeared in the root. If the first number is 5, and the next number is 7, then you should put 7 in the "right" root of node 5. And if you have 3, then 4, you must insert 3 to the "left" of 5, and then to 4 to the “correct” one from 3. If you cross this tree in order (always going left down the tree, we return only when there are no new nodes, and then to the right), you will get a sorted list: 3, 4, 5, 7.

  5 / \ 3 7 \ 4 

Here you can see the tree structure. If you did this in C, you would use structures that would look like this (I use pseudocode):

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

There is a lot of material about binary trees (which I explain), but first of all I wanted you to move away from the concept of arrays, "as dimensions in space", and much more only from 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 come up with an example now, but they have been used before.

+1
source

As physical three-dimensional creatures, we cannot “visualize” what 4, 5, 6 (or higher) physical dimensions represent.

The 4th dimension would increase our perception to the 4th direction , which would be orthogonal in the directions of height, width and depth, which we naturally perceive. Yes - the geometry was weird !!

In order to give us a sense of this idea, in this video, Karl Sagan imagines how he would feel like a perfectly flat 2-meter creature (small square) living in the 2nd world to meet a mysterious three-dimensional creature.
This three-dimensional creature (suspiciously like an apple) exists mainly in this mysterious third dimension, which a small square cannot “see”. He perceives only points of the apple that intersect with his 2d flat world, i.e. Its projection ...

The video looks old-fashioned by today's standards, but from the point of view of physics / geometry is still the best explanation that I saw there.

0
source

All Articles