0

There is a really good explanation of multi-dimensional array here on stackoverflow which I have studied and researched but i have few follow up questions for anyone who wants to help out. This is not a HW question, it is out of my text book which I am trying to understand more so please confirm if I am looking at the below example correctly. Thank you in advance.

So if i had a 3 dimensional array such as this:

                  {{{'1','2'},{'3','4'}},
                   {{'5','6'},{'7','8'}},
                   {{'9','10'},{'11','12'}}};

Would the one dimensional outcome (using c compiler) simply be?: +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+ | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | | | | | | | | | | | | | | +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+ And the corresponding position as? +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+ | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | | | | | | | | | | | | | | +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+

Again I am using this link as my source. The only thing I am looking for as a form of answer is, am I looking/doing this correctly? If not, I would appreciate it if you can tell me where I have made any mistakes. Thank you again.

Community
  • 1
  • 1
Anna B.
  • 3
  • 2

2 Answers2

1

1.

char [3][2][2] :

+-----+-----+         +-----+-----+
|+-----+-----+        |+-----+-----+
||   1 |   3 |        ||  4  |  5  |
||1,0+-----+-----+    ||   +-----+-----+
|+---|  a  |  b  |    |+---|  0  |  1  |
||  2|0,0,0|0,0,1|    ||  6|     |     |
+|1,1+-----+-----+ => +|   +-----+-----+
 +---|  x  |  y  |     +---|  2  |  3  |
     |0,1,0|0,1,1|         |     |     |
     +-----+-----+         +-----+-----+

so your outcome seems ok, and thus (2.) t3[0] should be a.

2.

if t2 looks like this, t2[0][1] is b:

+-----+-----+-----+-----+      +-----+-----+-----+-----+
|  a  |  b  |  x  |  y  |      |     |     |     |     |
|0,0,0|0,0,1|0,1,0|0,1,1|      | 0,0 | 0,1 | 0,2 | 0,3 |
+-----+-----+-----+-----+      +-----+-----+-----+-----+
|  1  |  3  |  2  |  7  |  =>  |     |     |     |     |
|1,0,0|1,0,1|1,1,0|1,1,1|      | 1,0 | 1,1 | 1,2 | 1,3 |
+-----+-----+-----+-----+      +-----+-----+-----+-----+
|  q  |  g  |  r  |  4  |      |     |     |     |     |
|2,0,0|2,0,1|2,1,0|2,1,1|      | 2,0 | 2,1 | 2,2 | 2,3 |
+-----+-----+-----+-----+      +-----+-----+-----+-----+

As long you are converting them the right way(as it seems according to the link) it should work...

XoMEX
  • 321
  • 2
  • 7
0

For conceptual understanding this is a good starting point.

But you should understand the difference between row vs column major. And technically it could vary between compilers and languages depending upon what they are designed for.

http://en.wikipedia.org/wiki/Row-major_order

fkl
  • 5,412
  • 4
  • 28
  • 68