There is a multidimensional array in the code. I need to count the number of rows in each group.
Can you please tell me how to count the number of rows correctly? I have tried different variants of sizeof(), using a for loop, but in the end I have not succeeded yet.
For example, I should have {5, 2, 4}
That cannot be calculated. You have allocated space for 15 rows (NUMBER_OF_GROUPS), the unused rows are filled with 0’s. With variable number of rows, better to declare each of the set of rows separately, the have a struct with a pointer to the data and a variable specifying the number of rows.
You have a three-dimension array with ROWS, COLS, OTHERS dimensions... so the number of "rows" would be three, "columns" would be thirteen, and "other" dimension would vary.
You could "discover" the number of elements (lines x columns) if you add "number of rows per row" as the first element of the two-dimension array and increase the second dimension by one... for example...
I changed your original data to show how the elements of the three-dimensions are addressed.
The code:
const byte NUMBER_OF_LEDS = 13;
const byte NUMBER_OF_GROUPS = 15;
unsigned int periods[][NUMBER_OF_GROUPS][NUMBER_OF_LEDS] = {
// 0 1 2 3 4 5 6 7 8 9 10 11 12 - elements
{
{2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3}, // line 0
{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{4, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 5} // line 4
},
{
{6, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 7},
{8, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 9}
},
{
{0xa, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xb},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
{0xc, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0xd}
},
};
void setup() {
Serial.begin(115200);
// first block of data
Serial.print("First array, first line, first element (periods[0][0][0]): ");
Serial.println(periods[0][0][0], HEX);
Serial.print("First array, first line, last element (periods[0][0][12]): ");
Serial.println(periods[0][0][12], HEX);
Serial.print("First array, last line, first element (periods[0][4][0]): ");
Serial.println(periods[0][4][0], HEX);
Serial.print("First array, last line, last element (periods[0][4][12]): ");
Serial.println(periods[0][4][12], HEX);
// second block of data
Serial.println();
Serial.print("Second array, first line, first element (periods[1][0][0]): ");
Serial.println(periods[1][0][0], HEX);
Serial.print("Second array, first line, last element (periods[1][0][12]): ");
Serial.println(periods[1][0][12], HEX);
Serial.print("Second array, last line, first element (periods[1][1][0]): ");
Serial.println(periods[1][1][0], HEX);
Serial.print("Second array, last line, last element (periods[1][1][12]): ");
Serial.println(periods[1][1][12], HEX);
// third block of data
Serial.println();
Serial.print("Third array, first line, first element (periods[2][0][0]): ");
Serial.println(periods[2][0][0], HEX);
Serial.print("Third array, first line, last element (periods[2][0][12]): ");
Serial.println(periods[2][0][12], HEX);
Serial.print("Third array, last line, first element (periods[2][3][0]): ");
Serial.println(periods[2][3][0], HEX);
Serial.print("Third array, last line, last element (periods[2][3][12]): ");
Serial.println(periods[2][3][12], HEX);
}
void loop() {
}
Result:
First array, first line, first element (periods[0][0][0]): 2
First array, first line, last element (periods[0][0][12]): 3
First array, last line, first element (periods[0][4][0]): 4
First array, last line, last element (periods[0][4][12]): 5
Second array, first line, first element (periods[1][0][0]): 6
Second array, first line, last element (periods[1][0][12]): 7
Second array, last line, first element (periods[1][1][0]): 8
Second array, last line, last element (periods[1][1][12]): 9
Third array, first line, first element (periods[2][0][0]): A
Third array, first line, last element (periods[2][0][12]): B
Third array, last line, first element (periods[2][3][0]): C
Third array, last line, last element (periods[2][3][12]): D
What is your ultimate goal? It looks like you are going for an LED pattern. I used a single dimension to color two,16x16 matrices of LEDs using TEXT as place holders... searching for it now... found it...
When you just store 0 and 1 why do you need an integer with two bytes instead of a simple uint8_t or byte?
When you just store 0 and 1 for 13 LEDs why do you need an array of 13 elements at all?
use one uint16_t and use each bit in the variable
0x1000000000000
Please explain what you want to achieve at all, there might be a more suitable solution than a multidimensional array.
There's no way to do that other than on your hands and knees looking for things you placed in the arrays in the initialiser.
As must have been said, what you didn't specific is automatically zero, there woukd be nothing anywhere to differentiate your initial values from the same ones replaced with text that spelled out all the values explicitly (adding many 0s).