# Switching array

hi

I probably approaching this from the wrong angle but hey ho ...i wondering if any one can help ....I'm old and not that clever.
The following code has two arrays woot and woot1.....to get them to work was an amazing achievement for my brain...

I wanted to be able to switch from one array to another obviously dependent on a variable ....
for example if x = 1 then it would step through woot ....and if x=2 it would step through woot1.

i intend to have quiet a few arrays

I've search array switching and nothing meaningful that i can understand comes up ....can anyone help ?
thanking you in advance ....
here's the code and my favorite joke at the moment...

What did the Zero say to the number Eight ? .................... Nice belt

``````int fromarray;
int index=0;

int woot[] = {2000, 3111, 3848, 3962, 3414, 2390, 1235, 337, 0, 337, 1235,};

int woot1[] = {0, 4000, 4000, 4000, 0, 0, 4000, 4000, 4000, 0, 0,};

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

}

void loop() {

fromarray = woot[index]; // this is where i want to be able to flip between woot and woot 1

Serial.println(fromarray);
delay(50);
//Serial.println(index);

index = index + 1;
if (index == 11){index=0;};

}
``````

Assuming the array's will be the same length, it sounds like you want a mulch-dimensional array:

``````int woot[11][2] = {
{ 2000, 3111, 3848, 3962, 3414, 2390, 1235, 337, 0, 337, 1235 },
{ 0, 4000, 4000, 4000, 0, 0, 4000, 4000, 4000, 0, 0, }
};
``````

That or an array pointer that you set depending on x:

int woot[5];
int woot1[5];

int *wootp;

if (x==1)
{
wootp = &woot[0];
} else {
wootp = &woot[1];
}

y = wootp[3];

blimey

thank you both for your replies.......

both work except i think Arrch it should read int woot[2][11] rather than int woot[11][2]

please please please say i'm correct .....this isn't to put you down honest .....because you are all fab ......but i might have just realized how it works..

beaming with pride

thanks again and again

oldman2758:
please please please say i'm correct .....this isn't to put you down honest .....because you are all fab ......but i might have just realized how it works..

I don't work with multidimensional arrays enough to pay attention to which ones goes first, so if that is the way that works, then I'm sure you're right.

Try this example:

``````#define MAXSIZE 12
int fromarray;
int index=0;

int woot[] = {2000, 3111, 3848, 3962, 3414, 2390, 1235, 337, 0, 337, 1235,};

int woot1[] = {0, 4000, 4000, 4000, 0, 0, 4000, 4000, 4000, 0, 0,};

void setup() {
Serial.begin(9600);
Serial.println("Enter a number: ");
}

void loop() {
char buffer[MAXSIZE];
int whichOne;
byte len;

// fromarray = woot[index]; // this is where i want to be able to flip between woot and woot 1
if (Serial.available() > 0) {
len = Serial.readBytesUntil('\r', buffer, MAXSIZE - 1);
buffer[len] = '\0';
whichOne = atoi(buffer);
if (whichOne % 2 == 0) {    // If it's an even number...
showArray(woot);          // ...show woot
} else {                    // If it's an odd number...
showArray(woot1);         // ...show woot1
}
}
}

void showArray(int array[])
{
int i;

Serial.println("Array contents: ");
for (i = 0; i < MAXSIZE; i++)
{
Serial.print("  ");
Serial.print(array[i]);
}
Serial.println();
}
``````

When the program begins, it asks you to enter a number. Open your IDE’s serial monitor (Tools → Serial Monitor, or Ctrl-Shift-M), and type in a number and press the Enter key. This feeds your input into the serial communications link where the program is polling the serial input via the call to Serial.available(). When that calls sees something, the call to Serial.readBytesUntil() reads a maximum of MAXSIZE - 1 bytes of data or until it senses the end of input (’\r’). The call to atoi() converts the character array data into a number (len). The modulo operator (%) determines if the number is odd or even, and the showArray() call passes in the appropriate array and displays its contents. While this may not be exactly what you want to do, perhaops you can modify it to suit your needs.

Or make these changes to econjack's code.

``````int x;

if (Serial.available() > 0) {
x = Serial.parseInt();
if (x % 2 == 0) {
showArray(woot);
}
else {
showArray(woot1);
}
``````
``````if (x==1)
{
wootp = &woot[0];
} else {
wootp = &woot[1];
}
``````

I think that second one should be:

``````   wootp = &woot1[0];
``````

Which is why woot and woot1 are such crappy names for the arrays - too easy to mess things up!

Thanks again to All

That is gonna take me a least a week to absorb all your ideas