Comparing multiple (more than two) arrays

Hello, so I have been stuck on this one big time. My goal is to compare input array with a few referent arrays. Furthermore, when input array is matched with one of the referent arrays it should do some task like for example serial print. I will post my code which doesn’t work bellow. Note: I have noticed that the code only compares first elements of arrays (without added if statement), not the all elements which doesn’t work for me. I also tried to put extra if statement but then it only compares 7th element in arrays.

int dataArray[7] = {1, 1, 0, 1, 0, 0, 1}  //input array (should be matched with n1)
int n0[7] = {0, 0, 0, 0, 0, 0, 0};        //this arrays represent number to compare with 
int n1[7] = {1, 1, 0, 1, 0, 0, 1};
int n2[7] = {0, 1, 0, 1, 0, 1, 0};
int n3[7] = {1, 0, 0, 0, 0, 1, 1};


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

void loop() {

    for (m = 0; m < 7; m++) {
     
      if (dataArray[m] == n0[m]) {
        if (m == 6) {
          Serial.println("digit: 0");
        }
      }
      else if (dataArray[m] == n1[m]) {
        if (m == 6) {
          Serial.println("digit: 1");
        }
      }
      else if (dataArray[m] == n2[m]) {
        if (m == 6) {
          Serial.println("digit: 2");
        }
      }
      else if (dataArray[m] == n3[m]) {
        if (m == 6) {
         Serial.println("digit: 3");
        }
     
      }
    }
  
}

I would really appriciate your help since I’m stuck here. :o

If you’re going to compare a lot of arrays, just write a simple wrapper that implements the “==” operator:

[color=#5e6d03]template[/color] [color=#434f54]<[/color][color=#00979c]class[/color] [color=#000000]T[/color][color=#434f54],[/color] [b][color=#d35400]size_t[/color][/b] [color=#000000]N[/color][color=#434f54]>[/color]
[color=#00979c]struct[/color] [color=#00979c]array[/color] [color=#000000]{[/color]
    [color=#000000]T[/color] [color=#000000]data[/color][color=#000000][[/color][color=#000000]N[/color][color=#000000]][/color][color=#000000];[/color]
    
    [color=#000000]T[/color] [color=#434f54]&[/color][color=#00979c]operator[/color][color=#000000][[/color][color=#000000]][/color][color=#000000]([/color][b][color=#d35400]size_t[/color][/b] [color=#000000]index[/color][color=#000000])[/color] [color=#000000]{[/color] [color=#5e6d03]return[/color] [color=#000000]data[/color][color=#000000][[/color][color=#000000]index[/color][color=#000000]][/color][color=#000000];[/color] [color=#000000]}[/color]
    [color=#00979c]const[/color] [color=#000000]T[/color] [color=#434f54]&[/color][color=#00979c]operator[/color][color=#000000][[/color][color=#000000]][/color][color=#000000]([/color][b][color=#d35400]size_t[/color][/b] [color=#000000]index[/color][color=#000000])[/color] [color=#00979c]const[/color] [color=#000000]{[/color] [color=#5e6d03]return[/color] [color=#000000]data[/color][color=#000000][[/color][color=#000000]index[/color][color=#000000]][/color][color=#000000];[/color] [color=#000000]}[/color]
    
    [color=#000000]T[/color] [color=#434f54]*[/color][color=#d35400]begin[/color][color=#000000]([/color][color=#000000])[/color] [color=#000000]{[/color] [color=#5e6d03]return[/color] [color=#434f54]&[/color][color=#000000]data[/color][color=#000000][[/color][color=#000000]0[/color][color=#000000]][/color][color=#000000];[/color] [color=#000000]}[/color]
    [color=#00979c]const[/color] [color=#000000]T[/color] [color=#434f54]*[/color][color=#d35400]begin[/color][color=#000000]([/color][color=#000000])[/color] [color=#00979c]const[/color] [color=#000000]{[/color] [color=#5e6d03]return[/color] [color=#434f54]&[/color][color=#000000]data[/color][color=#000000][[/color][color=#000000]0[/color][color=#000000]][/color][color=#000000];[/color] [color=#000000]}[/color]
    [color=#000000]T[/color] [color=#434f54]*[/color][color=#d35400]end[/color][color=#000000]([/color][color=#000000])[/color] [color=#000000]{[/color] [color=#5e6d03]return[/color] [color=#434f54]&[/color][color=#000000]data[/color][color=#000000][[/color][color=#000000]N[/color][color=#000000]][/color][color=#000000];[/color] [color=#000000]}[/color]
    [color=#00979c]const[/color] [color=#000000]T[/color] [color=#434f54]*[/color][color=#d35400]end[/color][color=#000000]([/color][color=#000000])[/color] [color=#00979c]const[/color] [color=#000000]{[/color] [color=#5e6d03]return[/color] [color=#434f54]&[/color][color=#000000]data[/color][color=#000000][[/color][color=#000000]N[/color][color=#000000]][/color][color=#000000];[/color] [color=#000000]}[/color]
    
    [color=#00979c]bool[/color] [color=#00979c]operator[/color][color=#434f54]==[/color][color=#000000]([/color][color=#00979c]const[/color] [color=#00979c]array[/color][color=#434f54]<[/color][color=#000000]T[/color][color=#434f54],[/color] [color=#000000]N[/color][color=#434f54]>[/color] [color=#434f54]&[/color][color=#000000]rhs[/color][color=#000000])[/color] [color=#00979c]const[/color] [color=#000000]{[/color]
        [color=#5e6d03]if[/color] [color=#000000]([/color][color=#5e6d03]this[/color] [color=#434f54]==[/color] [color=#434f54]&[/color][color=#000000]rhs[/color][color=#000000])[/color]
            [color=#5e6d03]return[/color] [color=#00979c]true[/color][color=#000000];[/color]
        [color=#5e6d03]for[/color] [color=#000000]([/color][b][color=#d35400]size_t[/color][/b] [color=#000000]i[/color] [color=#434f54]=[/color] [color=#000000]0[/color][color=#000000];[/color] [color=#000000]i[/color] [color=#434f54]<[/color] [color=#000000]N[/color][color=#000000];[/color] [color=#000000]i[/color][color=#434f54]++[/color][color=#000000])[/color]
            [color=#5e6d03]if[/color] [color=#000000]([/color][color=#000000]([/color][color=#434f54]*[/color][color=#5e6d03]this[/color][color=#000000])[/color][color=#000000][[/color][color=#000000]i[/color][color=#000000]][/color] [color=#434f54]!=[/color] [color=#000000]rhs[/color][color=#000000][[/color][color=#000000]i[/color][color=#000000]][/color][color=#000000])[/color]
                [color=#5e6d03]return[/color] [color=#00979c]false[/color][color=#000000];[/color]
        [color=#5e6d03]return[/color] [color=#00979c]true[/color][color=#000000];[/color]
    [color=#000000]}[/color]
    [color=#00979c]bool[/color] [color=#00979c]operator[/color][color=#434f54]!=[/color][color=#000000]([/color][color=#00979c]const[/color] [color=#00979c]array[/color][color=#434f54]<[/color][color=#000000]T[/color][color=#434f54],[/color] [color=#000000]N[/color][color=#434f54]>[/color] [color=#434f54]&[/color][color=#000000]rhs[/color][color=#000000])[/color] [color=#00979c]const[/color] [color=#000000]{[/color] [color=#5e6d03]return[/color] [color=#434f54]![/color][color=#000000]([/color][color=#434f54]*[/color][color=#5e6d03]this[/color] [color=#434f54]==[/color] [color=#000000]rhs[/color][color=#000000])[/color][color=#000000];[/color] [color=#000000]}[/color]
[color=#000000]}[/color][color=#000000];[/color]
template <class T, size_t N>
struct array {
    T data[N];
    
    T &operator[](size_t index) { return data[index]; }
    const T &operator[](size_t index) const { return data[index]; }
    
    T *begin() { return &data[0]; }
    const T *begin() const { return &data[0]; }
    T *end() { return &data[N]; }
    const T *end() const { return &data[N]; }
    
    bool operator==(const array<T, N> &rhs) const {
        if (this == &rhs)
            return true;
        for (size_t i = 0; i < N; i++)
            if ((*this)[i] != rhs[i])
                return false;
        return true;
    }
    bool operator!=(const array<T, N> &rhs) const { return !(*this == rhs); }
};

array<int, 7> dataArray = {{1, 1, 0, 1, 0, 0, 1}};  // input array (should be matched with n1)
array<int, 7> n0 = {{0, 0, 0, 0, 0, 0, 0}};  // these arrays represent number to compare with 
array<int, 7> n1 = {{1, 1, 0, 1, 0, 0, 1}};
array<int, 7> n2 = {{0, 1, 0, 1, 0, 1, 0}};
array<int, 7> n3 = {{1, 0, 0, 0, 0, 1, 1}};

void setup() {
  Serial.begin(115200);
  while(!Serial);
}

void loop() {
  if (dataArray == n0)
    Serial.println("digit: 0");
  else if (dataArray == n1)
    Serial.println("digit: 1");
  else if (dataArray == n2)
    Serial.println("digit: 2");
  else if (dataArray == n3)
    Serial.println("digit: 3");
  else
    Serial.println("digit: unknown");
  while(1);
}

However, this would be ridiculous if your arrays only contain zeros and ones. Just save them as bits in a byte:

uint8_t data = 0b1101001;  // input (should be matched with n1)
uint8_t n0 =   0b0000000;  // these represent numbers to compare with 
uint8_t n1 =   0b1101001;
uint8_t n2 =   0b0101010;
uint8_t n3 =   0b1000011;

void setup() {
  Serial.begin(115200);
  while(!Serial);
}

void loop() {
  if (data == n0)
    Serial.println("digit: 0");
  else if (data == n1)
    Serial.println("digit: 1");
  else if (data == n2)
    Serial.println("digit: 2");
  else if (data == n3)
    Serial.println("digit: 3");
  else
    Serial.println("digit: unknown");
  while(1);
}

Pieter

If the arrays can be declared as byte, use the function strcmp().

Otherwise, use a logical variable to record whether any of the individual elements do not match.

Something like this:

     byte equal = 1;
     for (m = 0; m < 7; m++) {
      if (dataArray[m] != n0[m])  equal=0;
      }
      Serial.println(equal);

Try this. It might give you some ideas

int dataArray[7] = {1, 1, 0, 1, 0, 0, 1};  //input array (should be matched with n1)
int n0[7] = {0, 0, 0, 0, 0, 0, 0};        //this arrays represent number to compare with
int n1[7] = {1, 1, 0, 1, 0, 0, 1};
int n2[7] = {0, 1, 0, 1, 0, 1, 0};
int n3[7] = {1, 0, 0, 0, 0, 1, 1};

void setup()
{
  Serial.begin(115200);
  byte compResult;
  compResult = memcmp( dataArray , n0, sizeof(dataArray));
  Serial.println(compResult);
  if (compResult == 0)
  {
    Serial.println("matched with array n0");
  }
  
  compResult = memcmp( dataArray , n1, sizeof(dataArray));
  Serial.println(compResult);
  if (compResult == 0)
  {
    Serial.println("matched with array n1");
  }
}

void loop()
{
}

Thank you guys for response. I'm trying to avoid using declaration as byte. Will try something with your suggestions...

DoxPz:
I'm trying to avoid using declaration as byte.

Why?

You could use something like this:

[color=#00979c]class[/color] [color=#000000]Digit[/color] [color=#000000]{[/color]
  [color=#00979c]private[/color][color=#434f54]:[/color]
    [color=#00979c]uint8_t[/color] [color=#000000]data[/color][color=#000000];[/color]
  [color=#00979c]public[/color][color=#434f54]:[/color]
    [color=#000000]Digit[/color][color=#000000]([/color][color=#00979c]bool[/color] [color=#000000]b6[/color][color=#434f54],[/color] [color=#00979c]bool[/color] [color=#000000]b5[/color][color=#434f54],[/color] [color=#00979c]bool[/color] [color=#000000]b4[/color][color=#434f54],[/color] [color=#00979c]bool[/color] [color=#000000]b3[/color][color=#434f54],[/color] [color=#00979c]bool[/color] [color=#000000]b2[/color][color=#434f54],[/color] [color=#00979c]bool[/color] [color=#000000]b1[/color][color=#434f54],[/color] [color=#00979c]bool[/color] [color=#000000]b0[/color][color=#000000])[/color]
      [color=#434f54]:[/color]  [color=#000000]data[/color][color=#000000]([/color][color=#000000]([/color][color=#000000]b6[/color] [color=#434f54]<<[/color] [color=#000000]6[/color][color=#000000])[/color] [color=#434f54]|[/color] [color=#000000]([/color][color=#000000]b5[/color] [color=#434f54]<<[/color] [color=#000000]5[/color][color=#000000])[/color] [color=#434f54]|[/color] [color=#000000]([/color][color=#000000]b4[/color] [color=#434f54]<<[/color] [color=#000000]4[/color][color=#000000])[/color] [color=#434f54]|[/color] [color=#000000]([/color][color=#000000]b3[/color] [color=#434f54]<<[/color] [color=#000000]3[/color][color=#000000])[/color] [color=#434f54]|[/color]
              [color=#000000]([/color][color=#000000]b2[/color] [color=#434f54]<<[/color] [color=#000000]2[/color][color=#000000])[/color] [color=#434f54]|[/color] [color=#000000]([/color][color=#000000]b1[/color] [color=#434f54]<<[/color] [color=#000000]1[/color][color=#000000])[/color] [color=#434f54]|[/color] [color=#000000]([/color][color=#000000]b0[/color] [color=#434f54]<<[/color] [color=#000000]0[/color][color=#000000])[/color][color=#000000])[/color] [color=#000000]{[/color][color=#000000]}[/color]
    [color=#00979c]bool[/color] [color=#00979c]operator[/color][color=#434f54]==[/color][color=#000000]([/color][color=#000000]Digit[/color] [color=#000000]rhs[/color][color=#000000])[/color] [color=#00979c]const[/color] [color=#000000]{[/color]
      [color=#5e6d03]return[/color] [color=#5e6d03]this[/color][color=#434f54]-[/color][color=#434f54]>[/color][color=#000000]data[/color] [color=#434f54]==[/color] [color=#000000]rhs[/color][color=#434f54].[/color][color=#000000]data[/color][color=#000000];[/color]
    [color=#000000]}[/color]
[color=#000000]}[/color][color=#000000];[/color]

[color=#000000]Digit[/color] [color=#000000]data[/color] [color=#434f54]=[/color] [color=#000000]{[/color][color=#000000]1[/color][color=#434f54],[/color] [color=#000000]1[/color][color=#434f54],[/color] [color=#000000]0[/color][color=#434f54],[/color] [color=#000000]1[/color][color=#434f54],[/color] [color=#000000]0[/color][color=#434f54],[/color] [color=#000000]0[/color][color=#434f54],[/color] [color=#000000]1[/color][color=#000000]}[/color][color=#000000];[/color]  [color=#434f54]//input array (should be matched with n1)[/color]
[color=#000000]Digit[/color] [color=#000000]n0[/color] [color=#434f54]=[/color] [color=#000000]{[/color][color=#000000]0[/color][color=#434f54],[/color] [color=#000000]0[/color][color=#434f54],[/color] [color=#000000]0[/color][color=#434f54],[/color] [color=#000000]0[/color][color=#434f54],[/color] [color=#000000]0[/color][color=#434f54],[/color] [color=#000000]0[/color][color=#434f54],[/color] [color=#000000]0[/color][color=#000000]}[/color][color=#000000];[/color]        [color=#434f54]//this arrays represent number to compare with[/color]
[color=#000000]Digit[/color] [color=#000000]n1[/color] [color=#434f54]=[/color] [color=#000000]{[/color][color=#000000]1[/color][color=#434f54],[/color] [color=#000000]1[/color][color=#434f54],[/color] [color=#000000]0[/color][color=#434f54],[/color] [color=#000000]1[/color][color=#434f54],[/color] [color=#000000]0[/color][color=#434f54],[/color] [color=#000000]0[/color][color=#434f54],[/color] [color=#000000]1[/color][color=#000000]}[/color][color=#000000];[/color]
[color=#000000]Digit[/color] [color=#000000]n2[/color] [color=#434f54]=[/color] [color=#000000]{[/color][color=#000000]0[/color][color=#434f54],[/color] [color=#000000]1[/color][color=#434f54],[/color] [color=#000000]0[/color][color=#434f54],[/color] [color=#000000]1[/color][color=#434f54],[/color] [color=#000000]0[/color][color=#434f54],[/color] [color=#000000]1[/color][color=#434f54],[/color] [color=#000000]0[/color][color=#000000]}[/color][color=#000000];[/color]
[color=#000000]Digit[/color] [color=#000000]n3[/color] [color=#434f54]=[/color] [color=#000000]{[/color][color=#000000]1[/color][color=#434f54],[/color] [color=#000000]0[/color][color=#434f54],[/color] [color=#000000]0[/color][color=#434f54],[/color] [color=#000000]0[/color][color=#434f54],[/color] [color=#000000]0[/color][color=#434f54],[/color] [color=#000000]1[/color][color=#434f54],[/color] [color=#000000]1[/color][color=#000000]}[/color][color=#000000];[/color]

jremington:
If the arrays can be declared as byte, use the function strcmp().

Good advice except if 0 (zero) can appear in the data. strcmp() will stop at the first zero byte.

MorganS:
Good advice except if 0 (zero) can appear in the data. strcmp() will stop at the first zero byte.

memcmp() takes a length parameter to control the search. That would work, no?

dougp:
memcmp() takes a length parameter to control the search. That would work, no?

See reply #3

UKHeliBob:
See reply #3

Oops. Still commenting without reading the responses! smiley uh oh.PNG

smiley uh oh.PNG

He caught both of us there.