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);
}
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();
}
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);
}
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()
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
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);
}
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
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.
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