Frustrated - How Initialize a 2D Array

I'm struggling on how a 2D array is initialized. I think the rest of my code will work if I could just get the two lines of code working properly. I've tried many different {} combinations but the data is never entered correctly.

int TriStatus[3][16] = {0,1,0}, {0,1,0}, {0,1,0}, {0,1,0}, {0,1,0}, {0,1,0}, {0,1,0}, {0,1,0},  {1,0,0}, {0,1,0}, {0,1,0}, {0,1,0}, {0,1,0}, {0,1,0}, {0,1,0}, {0,1,0};

int PosServo[3][16] = {800,1050,1250, 900,1000,1100, 900,1000,1100, 900,1000,1100, 900,1000,1100, 900,1000,1100, 900,1000,1100, 900,1000,1100,  900,1100,1100, 900,1000,1100, 900,1000,1100, 900,1000,1100, 900,1000,1100, 900,1000,1100, 900,1000,1100, 900,1000,1100};

I want the first three numbers to fill the rows then the next fille the next columns three rows.
would it be better to go the "PosServo[16][3]" route? I've tried this to but cannot figure out what the correct format is to initialize ether.

byte TriStatus[5] = {B010,B010,B010,B010,B010}; three bits are status state holder

bool TriStatus[16][3] = {{0,1,0}, {0,1,0}, {0,1,0}, {0,1,0}, {0,1,0}, {0,1,0}, {0,1,0}, {0,1,0}, {1,0,0}, {0,1,0}, {0,1,0}, {0,1,0}, {0,1,0}, {0,1,0}, {0,1,0}, {0,1,0}};
bool TriStatus[16][3] = {0,1,0, 0,1,0, 0,1,0, 0,1,0, 0,1,0, 0,1,0, 0,1,0, 0,1,0, 1,0,0, 0,1,0, 0,1,0, 0,1,0, 0,1,0, 0,1,0, 0,1,0, 0,1,0};

Hello ajkochev
Take a view here to gain the knowledge:

Have a nice day and enjoy programming in C++ and learning.
Дайте миру шанс!

I've read through this all morning. I'm still not getting the right population in the array from my understanding of the article. It's not clear enough for a dummy like me. I'm hoping someone can correct my two lines of code so I can see a correct working example.

Correct initializations are shown in post #2.

I think you have your indexes reversed. You want 16 rows of 3 values.

Elements in two-dimensional arrays are commonly referred to by x[i][j] where i is the row number and ‘j’ is the column number.

would it be better to go the "PosServo[16][3]" route?
Yes, I think that is what you want.

Thank you. I have tried this way in the beginning but it still didn't initalize. Would this be correct? In that Column 0 rows 0 to 2 would populate then column 1 rows 0 to 2?

int TriStatus[16][3] = {{0,1,0},{0,1,0},{0,1,0},{0,1,0},{0,1,0},{0,1,0},{0,1,0},{0,1,0}, {1,0,0},{0,1,0},{0,1,0},{0,1,0},{0,1,0},{0,1,0},{0,1,0},{0,1,0}};

int PosServo[16][3] = {{800,1050,1250},{900,1000,1100},{900,1000,1100},{900,1000,1100},{900,1000,1100},{900,1000,1100},{900,1000,1100},{900,1000,1100} ,{900,1100,1100},{900,1000,1100},{900,1000,1100},{900,1000,1100},{900,1000,1100},{900,1000,1100},{900,1000,1100},{900,1000,1100}};
TriStatus[0][0]=0
TriStatus[0][1]=1
TriStatus[0][2]=0

and so on.

That formulation looks correct to me. What issues do you see?

Please explain. There is nothing wrong with those two lines.

Post the rest of your program.

Thanks, everyone for the help. Looks like this is correct. It was another part of the program that was totally off. The array is working correctly.

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