How to compare two arrays

Hi .

I'm doing some tests on how to compare two arrays but I'm not succeeding, I've read a lot about arrays and a lot of research I found nothing that could help me!
Hope you can help me please.
Thank you very much in Advance

Here is the code I have been testing but unsuccessfully

int array1[6] = {0,1,2,3,4,5}; // sample arrays
int array2[6] = {0,1,2,3,4,5};

int test2,test1;

static boolean result;  // this variable will hold the compare result

void setup()
    {
     Serial.begin(9600); // stablish serial communication
      }
  
  void loop(){

  
  for (int x=0; x<6; x++){ 
   test1 = array1[x];// asign each index of arrays to test, one by one and compare
   test2= array2[x];
   }
  
  if(test1!= test2){ // if any index comparison is not equal, then the arrays are not equal and result will be 0.
   result = false; 
  }else{ 
   result = true;
  }
  Serial.println(result); 
  delay(100);
 }

The test must be done within the loop braces.

For example:

 for (int x=0; x<6; x++){
   test1 = array1[x];// asign each index of arrays to test, one by one and compare
   test2= array2[x];
   if (test1 != test2) do_something();
   }

Thanks for answering !

I made this change but it didn't work

int array1[6] = {0,10,2,3,4,5}; // sample arrays
int array2[6] = {0,1,2,3,4,5};
int test2,test1;
static boolean result;  // this variable will hold the compare result

void setup()
    {
     Serial.begin(9600); // stablish serial communication
      }
  
  void loop(){

  
  for (int x=0; x<6; x++){ 
   test1 = array1[x];// asign each index of arrays to test, one by one and compare
   test2= array2[x];
     if(test1!= test2){ // if any index comparison is not equal, then the arrays are not equal and result will be 0.
      result = false; 
      }else{ 
      result = true;
      }
   }
  
  Serial.println(result); 
  delay(100);
 }

Try:

int array1[6] = {0, 1, 2, 3, 4, 5}; // sample arrays
int array2[6] = {0, 1, 2, 3, 4, 5};
int test2, test1;
static boolean result;  // this variable will hold the compare result

void setup()
{
  Serial.begin(9600); // stablish serial communication
}

void loop()
{
  int result = 0;
  for (int x = 0; x < 6; x++)
  {
    test1 = array1[x];// asign each index of arrays to test, one by one and compare
    test2 = array2[x];
    if (test1 != test2) // if any index comparison is not equal, then the arrays are not equal and result will be 0.
    {
      //found a bad one
      result++;
    }
  }

  Serial.println(result);
  delay(100);
  
} //END of loop()

Hi Guys.

Better not let spycatcher2k read this! :zipper_mouth_face:

Already changed

Thanks for answering but still without making the comparison!
What I really want the code to do is if the arrays are equal it returns number 1 otherwise it returns 0

You can invert things as needed:

int array1[6] = {0, 1, 2, 3, 4, 5}; // sample arrays
int array2[6] = {0, 1, 2, 3, 4, 5};
int test2, test1;

bool result;            //this variable will hold the compare result

void setup()
{
  Serial.begin(9600);   //establish serial communication
}

void loop()
{
  result = false;
  
  for (int x = 0; x < 6; x++)
  {
    test1 = array1[x];  //asign each index of arrays to test, one by one and compare
    test2 = array2[x];
    if (test1 != test2) //if any index comparison is not equal, then the arrays are not equal and result will be 0.
    {
      //found a bad one
      result = true;
    }
  }

  Serial.println(result);
  delay(100);
  
} //END of loop()

What I really want the code to do is if the arrays are equal it returns number 1 otherwise it returns 0

If you are only interested in an overall match, and don't care about specific position of any mismatches, you can use memcmp which compares two blocks of memory. memcmp returns 0 if there is a match, so you will need to invert the result.

int array1[6] = {0, 10, 2, 3, 4, 5}; // sample arrays
int array2[6] = {0, 1, 2, 3, 4, 5};
int test2, test1;
byte result;  // this variable will hold the compare result

void setup()
{
  Serial.begin(9600); // establish serial communication
}

void loop() {

  if (memcmp(array1, array2, sizeof(array1)) == 0)
    result = 1;
  else
    result = 0;

  Serial.println(result);
  delay(1000);
}

Thanks larryd for the help

But I would like you to explain why this code does not work I do not understand the reason, because logically it should work

int array1[6] = {0, 1, 2, 3, 4, 5}; // sample arrays
int array2[6] = {0, 1, 2, 3, 4, 5};
int test2, test1;

bool result;            //this variable will hold the compare result

void setup()
{
  Serial.begin(9600);   //establish serial communication
}

void loop()
{
  //result = false;
  
  for (int x = 0; x < 6; x++)
  {
    test1 = array1[x];  //asign each index of arrays to test, one by one and compare
    test2 = array2[x];
    if (test1 != test2) //if any index comparison is not equal, then the arrays are not equal and result will be 0.
    {
     result = true;}
      else {
      result = false;}
  }

  Serial.println(result);
  delay(100);
  
} //END of loop()

But I would like you to explain why this code does not work I do not understand the reason, because logically it should work

You overwrite the value of result with each iteration of the for loop.

cattledog:
You overwrite the value of result with each iteration of the for loop.

Thanks for helping

Fiasgardone:
Thanks larryd for the help

But I would like you to explain why this code does not work I do not understand the reason, because logically it should work

Your code, as well as larry's shouldn't work. You need to break out of the loop as soon as you find a mismatch, otherwise the result would just be overwritten by the last computation.

if (test1 != test2)
{
  result = true;
}
else
{
  result = false;
}

Look at what your 'else' section does.

larryd:

if (test1 != test2)

{
 result = true;
}
else
{
 result = false;
}





Look at what your 'else' section does.

By logic after the test"if (test1 != test2)" , if true returns 1 otherwise returns 0

   test1 = array1[x]; 
    test2 = array2[x];
    if (test1 != test2) 
    {
     result = true;}
      else {
      result = false;}
  }

arduino_new:
Your code, as well as larry's shouldn't work. You need to break out of the loop as soon as you find a mismatch, otherwise the result would just be overwritten by the last computation.

I could understand your explanation, can you give more details of what you mean?
Thank you very much in advance

2020-01-01_13-42-23.jpg

2020-01-01_13-42-23.jpg

Get rid of all of the "else" clause.
If you get rid of just the 'else"... Well, let's leave that as an exercise for the reader :smiley:

it is difficult to understand comparisons for me but let's see if I can understand

Just use memcmp; why reinvent the wheel?

TheMemberFormerlyKnownAsAWOL:
Just use memcmp; why reinvent the wheel?

yes, but it is always good to know all the features offered by the C language.