Two-dimensional arrays

There is little to no documentation on multi-dimensional arrays so I wanted to confirm two things before I start writing a YT video script explaining what they are and why they're so weird.

  1. When I'm getting a value in an array, Y comes before X, yes? Like if I were to use this:
int array1[13][8] = { 
  {0, 1, 2, 3, 4, 5, 6, 7},
  {1, 97, 109, 121, 92, 64, 245, 101},
  {2, 98, 110, 122, 124, 36, 241, 119},
  {3, 99, 111, 43, 60, 38, 49, 114},
  {4, 100, 112, 45, 62, 39, 50, 97},
  {5, 101, 113, 95, 63, 34, 51, 115},
  {6, 102, 114, 40, 46, 35, 52, 100},
  {7, 103, 115, 41, 33, 96, 53, 27},
  {8, 104, 116, 91, 44, 94, 54, 11},
  {9, 105, 117, 93, 126, 8, 55, 20},
  {10, 106, 118, 123, 59, 27, 56, 33},
  {11, 107, 119, 124, 58, 9, 57, 63},
  {12, 108, 120, 47, 42, 20, 48, 47}
};

void setup(){
  Serial.begin(9600);
}

void loop(){
  Serial.println(array1[3][2]);
}

I would get an output of 111 on serial? I'm asking because when I run this code on TinkerCAD it gives that output, but TCAD Circuits doesn't have the most reliable systems in place.

  1. I wouldn't assume you could make arrays with 3+ dimensions...

I think your questions explains well, why a struct is far more readable than a multi-dimensional array.
If you really want to make a yt video, consider to explain a structure of variables!

Look at that as 13 arrays of 8 elements each. To get the 3rd element of the 6th array you would use:

element = array1[5][2];  // arrays are numbered from 0

EDIT:
As pointed out by @runaway_pancake the above should be

element = array1[6][3];  // arrays are numbered from 0

to be consistent with the array and element numbering from the xero-eth.

1 Like

Google turns up "About 917,000 results" for 'C++ multidimensional arrays'.

https://www.tutorialspoint.com/cplusplus/cpp_multi_dimensional_arrays.htm

I'm using an application at the moment where a struct{} makes little to no sense. See those numbers on the Y axis? Those are button input IDs. The numbers on the X axis are rotary encoder counter values and everything else is ASCII codes.

So Keyboard.write(array1[2][6] would result in an output of ASCII 114.

EDIT - That means that if the rotary encoder's counter was reading "6" and I pressed button "2", the ASCII output would be "114".

EDIT - Not 114. Should be 241. Thanks @groundFungus. I do things too fast for my own good.

Why do you think they're "weird" ?

If you go forward with a YT video and that premise, others are going to assume they're "weird" too

@johnwasser Most of them explain them as a "table" which made no sense to me. Once I decided to look at them as coordinate planes they started making sense.

@TheMemberFormerlyKnownAsAWOL The way they behave is odd to me. Y before X, counting from 0 (that's why my code has placeholder values), that sort of thing. As for the impression on other people, the goal is to explain why they are hard to understand then then proceed to make them not hard to understand. Title would be "Demystifying Multidimensional Arrays (Arduino Tutorial)" or something like that.

Again, that kind-of assumes that they're mysterious to begin with.

The syntax is wrong.

Keyboard.write(array1[2][6] )  // corrected syntax
`

And that is the 6th element of array 2 or 241. Remember arrays and array elements are number from 0.

int array1[13][8] = { 
  {0, 1, 2, 3, 4, 5, 6, 7},
  {1, 97, 109, 121, 92, 64, 245, 101},
  {2, 98, 110, 122, 124, 36, **241**, 119},
  {3, 99, 111, 43, 60, 38, 49, 114},
  {4, 100, 112, 45, 62, 39, 50, 97},

@TheMemberFormerlyKnownAsAWOL I can't be the only one that gets confused by them.

@groundFungus This is what happens when you write code in a forum post in 30 seconds.

In the days before portable computing power became common, books of tables were published for all sorts of functions, like logarithms, circular functions, powers, roots and reciprocals.

I don't see your code, so I can't give an answer on that.
But when you can explain your data as "those are button input ids" and "rotary encoder counters", I see a data[0].id and data[0].counter.

@TheMemberFormerlyKnownAsAWOL Okay...
@noiasca How would I get an ASCII from that?

This is what happens when you reply too quickly

ASCII is 0 to 127.

I'm afraid I don't understand what you're trying to state.

Valid ASCII characters are 0 to 127, so 241 couldn't be ASCII.
That would be extended ASCII

Uh huh. I wanted to include a section sign and the plus/minus sign because I had too many buttons.

EDIT - § and ±

Well, you can, but you'll run out of memory that little bit quicker

1 Like

may be it's time you explain what you want to achieve in your sketch and give a working example.

because based on your data, I wonder why you need in index 0 numbers from 0 to 12.
This is already the index itself, so you are vasting RAM for 12 integers.

```
int array1[13][8] = { 
  {/*0,*/ 1, 2, 3, 4, 5, 6, 7},
  {/*1,*/ 97, 109, 121, 92, 64, 245, 101},
  {/*2,*/ 98, 110, 122, 124, 36, 241, 119},
  {/*3,*/ 99, 111, 43, 60, 38, 49, 114},
  {/*4,*/ 100, 112, 45, 62, 39, 50, 97},
  {/*5,*/ 101, 113, 95, 63, 34, 51, 115},
  {/*6,*/ 102, 114, 40, 46, 35, 52, 100},
  {/*7,*/ 103, 115, 41, 33, 96, 53, 27},
  {/*8,*/ 104, 116, 91, 44, 94, 54, 11},
  {/*9,*/ 105, 117, 93, 126, 8, 55, 20},
  {/*10,*/ 106, 118, 123, 59, 27, 56, 33},
  {/*11,*/ 107, 119, 124, 58, 9, 57, 63},
  {/*12,*/ 108, 120, 47, 42, 20, 48, 47}
};

if you only want to store chars, why are you using a two byte integer ?