Selecting an Array based on input

Hello (from a brand newby)

I have a series of arrays, say 1 to 5, each of 40 integers.
e.g.

int Offset_1[40] = {6, 19, 32, 6, 19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int Offset_2[40] = {13, 25, 38, 6, 19, 32, 13, 16, 38, 6, 19, 32, 13, 16, 38, 6, 19, 32, 6, 19, 32, 6, 16, 25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int Offset_3[40] = {13, 25, 38, 6, 19, 32, 13, 25, 38, 6, 19, 32, 13, 25, 38, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int Offset_4[40] = {6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 16, 25};
int Offset_5[40] = {6, 19, 32, 6, 19, 32, 6, 19, 32, 13, 25, 38, 6, 19, 32, 13, 25, 38, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 16, 25, 6, 16, 25, 0, 0, 0, 0, 0, 0};

An integer input is requested by the sketch, say 1 to 5. Input received, say "3".

Then, based on the input received, I would like to -

Copy CurrentOffset[40] = Offset_3[40]

Would someone be kind enough to let me know how I can accomplish this? I'd be grateful.

I do hope that I have posted this question in the right place, if not, then please let me know where should I post this question.

Thank You
Ajay Talwar

Instead of using 5 dimensional matrices, use a 2 dimensional matrix.
like this:

 int Offset[5][40] = {{6, 19, 32, 6, 19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
 {13, 25, 38, 6, 19, 32, 13, 16, 38, 6, 19, 32, 13, 16, 38, 6, 19, 32, 6, 19, 32, 6, 16, 25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
 {13, 25, 38, 6, 19, 32, 13, 25, 38, 6, 19, 32, 13, 25, 38, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0},
 {6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 16, 25},
 {6, 19, 32, 6, 19, 32, 6, 19, 32, 13, 25, 38, 6, 19, 32, 13, 25, 38, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 16, 25, 6, 16, 25, 0, 0, 0, 0, 0, 0},
  };
1 Like

indeed, and of course remember that index numbering starts at 0, so for

you need to get Offset[input-1][xxx]to address the xxxth entry of the array (again starting at 0)

1 Like

Welcome to the forum

Why not declare the array as 2 dimensional and pick the row to be used based on the number input ?

int offsetArray[][40] = {
    { 6, 19, 32, 6, 19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
    { 13, 25, 38, 6, 19, 32, 13, 16, 38, 6, 19, 32, 13, 16, 38, 6, 19, 32, 6, 19, 32, 6, 16, 25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
    { 13, 25, 38, 6, 19, 32, 13, 25, 38, 6, 19, 32, 13, 25, 38, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
    { 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 16, 25 },
    { 6, 19, 32, 6, 19, 32, 6, 19, 32, 13, 25, 38, 6, 19, 32, 13, 25, 38, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 16, 25, 6, 16, 25, 0, 0, 0, 0, 0, 0 }
};

void setup()
{
    Serial.begin(115200);
    Serial.println(offsetArray[3][4]); 
}

void loop()
{
}
1 Like

:slight_smile:

Thank You folks, its so easy once you know!

Best to all those who replied.
Ajay Talwar

otherwise a switch/case would do (and a pointer if you want to keep track of the selected array) :slight_smile:

Hi Jackson,

Would you have time to explain this a bit more?

Ajay

have a look at this (I modified the first value of your arrays so that it's easier to see what's being read)


int Offset_1[40] = {11, 19, 32, 6, 19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int Offset_2[40] = {22, 25, 38, 6, 19, 32, 13, 16, 38, 6, 19, 32, 13, 16, 38, 6, 19, 32, 6, 19, 32, 6, 16, 25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int Offset_3[40] = {33, 25, 38, 6, 19, 32, 13, 25, 38, 6, 19, 32, 13, 25, 38, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int Offset_4[40] = {44, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 16, 25};
int Offset_5[40] = {55, 19, 32, 6, 19, 32, 6, 19, 32, 13, 25, 38, 6, 19, 32, 13, 25, 38, 6, 19, 32, 6, 19, 32, 6, 19, 32, 6, 16, 25, 6, 16, 25, 0, 0, 0, 0, 0, 0};

int (*arrayPtr)[40] = nullptr;

void selectArray(byte arrayNb) {
  switch (arrayNb) {
    case 1: arrayPtr = &Offset_1; break;
    case 2: arrayPtr = &Offset_2; break;
    case 3: arrayPtr = &Offset_3; break;
    case 4: arrayPtr = &Offset_4; break;
    case 5: arrayPtr = &Offset_5; break;
    default: arrayPtr = nullptr; break;
  }
}

void setup() {
  Serial.begin(115200);

  for (byte i = 0; i <= 6; i++) {
    selectArray(i);
    Serial.print("first element of array Offset_");
    Serial.print(i);
    Serial.print(" is: ");
    if (arrayPtr != nullptr) Serial.println((*arrayPtr)[0]);
    else Serial.println("not existing.");
  }
}

void loop() {}

output on the serial monitor (set at 115200 bauds)

first element of array Offset_0 is: not existing.
first element of array Offset_1 is: 11
first element of array Offset_2 is: 22
first element of array Offset_3 is: 33
first element of array Offset_4 is: 44
first element of array Offset_5 is: 55
first element of array Offset_6 is: not existing.
1 Like

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