check ALL if the elements in an array meets a condition

Status
Not open for further replies.

Lorenzo

Well-known member
Hello forum,

I am trying to check if ALL the elements of an array meets a condition.

the pseudocode should be:

if (all the elements in the array > value)
{
do something;
}


I don't need a check for every element like the following that I have already tryed:


for (uint i = 0; i < 6; i++)
{
if (array_element> value)
{
do something;
}
}

but a check on the complete array.

Any suggestions on how to solve the problem?

Thank you very much
 
define how you mean check for entire array? do you want 100% of the items to be above value in order to fire the command?
 
Here you go buddy:)

Code:
  uint8_t myArray[10] = { 10, 11, [COLOR="#FF0000"]2[/COLOR], 13, 14, 15, 16, 17, 18, 19 };
  bool fail_check = 0;
    for (uint16_t i = 0; i < sizeof(myArray)/sizeof(myArray[0]); i++) {
    if (myArray[i] [COLOR="#FF0000"]< 9[/COLOR]) {
      fail_check = 1;
      break;
    }
  }
  ( fail_check ) ? Serial.println("FAIL") : Serial.println("PASS");
 
Status
Not open for further replies.
Back
Top