Counting instances in a multi-dimensional array

Hi - I have a table of arrays (dunno if that's the correct terminology) and I would like to count/reference specific items in each. I've highlighted the things I'd like to count:

int blNum;
int blMax = 15; 
struct Blocks
{
    int blNum;
    int blStatus;
    long blWinds;
    int blRPM;
    int blAccel;
    int blAreaL;
    int blAreaR;
    int blScatter;
    int blWPS;
    float ptArea;
    float ptInterval;
    int ptSpeed;
} const BlockTable[] =
{
{1, **1**, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0},
{2, **1**, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0},
{3, **1**, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0},
{4, **0**, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0},
{5, **0**, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0},
{6, **0**, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0},
{7, **0**, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0},
{8, **0**, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0},
{9, **0**, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0},
{10, **0**, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0},
{11, **0**, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0},
{12, **0**, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0},
{13, **0**, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0},
{14, **0**, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0},
{15, **0**, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0},
};
Blocks x = BlockTable[blNum - 1];

This is the code I have trying to count them - the number should come out to 3 if done correctly:

void blockManager() {
  for (int i = 0; i < blMax; i ++) {
    if (BlockTable[i][1] != 0) {
      blocksTotal = blocksTotal + 1;
    };
  };
};

Been trying to use this resource as a guide. I get the following compile error:

/Users/jtb/Downloads/AC-2_Current/AC-2_Current.ino: In function 'void blockManager()':
AC-2_Current:1431:22: error: no match for 'operator[]' (operand types are 'const Blocks' and 'int')
if (BlockTable[i][2] != 0) {
^
Multiple libraries were found for "WiFi.h"
Used: /Users/jtb/Library/Arduino15/packages/esp32/hardware/esp32/1.0.6/libraries/WiFi
Not used: /Applications/Arduino.app/Contents/Java/libraries/WiFi
exit status 1
no match for 'operator[]' (operand types are 'const Blocks' and 'int')

I put that little "Blocks x = BlockTable[blNum - 1];" line up there for later use, but maybe that could be useful here? I just don't know how to go about it. Thank you.

//Woops, apparently I can't make text bold in code formatting, sorry about that.

That is an array of struct, you would use BlockTable[i].blStatus to access that field.

1 Like

I can't figure out what you're trying to do with that line.

Damn you were a second faster than me - I just tried that and it compiled. I have another quick question though, should I do BlockTable[i - 1] to get the referencing to start at row 0?

That's for later use when I want to cycle through a chosen row. @david_2018 has helped find the solution for moving down a column.

No, when i = 0 you are referencing row 0. Some people prefer to think of the first row as 1, and will always start their for loop at 1 for that reason.

OK great, I'm a little rusty and couldn't remember if it immediately adds one or runs with i = 0 first. Thanks!

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