Novice question on syntax in sample code

Hi,

I'm new to the IDE and just started working on examples from an Arduino Uno starter kit.
There is a variable declaration with syntax I've not seen before.
Can someone explain what "[10][10]" is doing in the code below?
I've done some searching but haven't found any explanation yet.

int a[10][10]={
{0,0,0,1,1,1,0,1,1,1}, //0
{0,0,0,1,0,0,0,0,0,1}, //1
{0,0,0,0,1,1,1,0,1,1}, //2
{0,0,0,1,1,0,1,0,1,1}, //3
{0,0,0,1,0,0,1,1,0,1}, //4
{0,0,0,1,1,0,1,1,1,0}, //5
{0,0,0,1,1,1,1,1,1,0}, //6
{0,0,0,1,0,0,0,0,1,1}, //7
{0,0,0,1,1,1,1,1,1,1}, //8
{0,0,0,1,1,0,1,1,1,1},}; //9

Thanks

That defines a 2-dimention array with 10 items per row and 10 rows.

Welcome to the forum

The [10[10] tells the compiler that the variable a is an array with 2 dimensions. 10 rows and 10 columns

You would address the data held in the third column of the eighth row as a[7][2]

Note that the row and column numbers start at 0 and not 1

You need to read up on arrays in C

Thanks you both for your explanations.
Now the syntax makes sense me.
Before your help, I only had a vague idea what was going.

Hello bogusone

Welcome to the world's best Arduino forum ever.

Imagine a warehouse with 10 shelves, each with 10 compartments for storing data.

Have a nice day and enjoy coding in C++.

Thanks again...
Now I'm reminded that I've often been told "it's just syntax".
but... I do seem to spend most of my time figuring out syntax.

you may find the following helpful for this and other issues

The C Programming Language pg 99

Yes, this is/will be helpful.
Thanks

It's also very wasteful.

In these times of 16-core, 64-bit, 5GHz PC processors with 32 or 64GB of ram and 2 or 4 TB of storage, most people don't care about being wasteful any more. But Arduino programming still requires you to think about efficiency.

This array contains 10x10 int variables. Those use 16 bits of memory each, but only values 0 and 1 are used, which needs only 1 bit. So this way of storing the data is only 6% efficient, and 94% is wasted. It takes up 200 bytes of memory.

int a[10][10]={
{0,0,0,1,1,1,0,1,1,1}, //0
{0,0,0,1,0,0,0,0,0,1}, //1
{0,0,0,0,1,1,1,0,1,1}, //2
{0,0,0,1,1,0,1,0,1,1}, //3
{0,0,0,1,0,0,1,1,0,1}, //4
{0,0,0,1,1,0,1,1,1,0}, //5
{0,0,0,1,1,1,1,1,1,0}, //6
{0,0,0,1,0,0,0,0,1,1}, //7
{0,0,0,1,1,1,1,1,1,1}, //8
{0,0,0,1,1,0,1,1,1,1},}; //9

One easy way of making it more efficient would be to use byte instead of int.

byte a[10][10]={
{0,0,0,1,1,1,0,1,1,1}, //0
{0,0,0,1,0,0,0,0,0,1}, //1
{0,0,0,0,1,1,1,0,1,1}, //2
{0,0,0,1,1,0,1,0,1,1}, //3
{0,0,0,1,0,0,1,1,0,1}, //4
{0,0,0,1,1,0,1,1,1,0}, //5
{0,0,0,1,1,1,1,1,1,0}, //6
{0,0,0,1,0,0,0,0,1,1}, //7
{0,0,0,1,1,1,1,1,1,1}, //8
{0,0,0,1,1,0,1,1,1,1},}; //9

This would be 12% efficient and would only waste 88%. Now it takes up only 100 bytes of memory.

This change would make a much bigger difference

int a[10]={
0b0001110111, //0
0b0001000001, //1
...
0b0001101111 }; //9

This would be 62% efficient and waste only 38%. It takes 20 bytes of memory. But it would require changes to the rest of the code, because, as you can see, it's now only a 1-dimensional array, with the second dimension packed bit-wise into int variables.

1 Like

Good point.

For what I think this is intended for (a 7-segment display) , using 10 bytes would be more efficient yet. Waste would be even less

why not

//    a      d
//   f b    c e
//    g      g
//   e c    b f
//    d h    a
//
//   h g f e d c b a

const byte SEGMENT_MAP_DIGIT[] = {
    0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0X80, 0X90
};
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.