Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 4 of 4

Thread: check ALL if the elements in an array meets a condition

  1. #1

    check ALL if the elements in an array meets a condition

    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[i]> value)
    {
    do something;
    }
    }

    but a check on the complete array.

    Any suggestions on how to solve the problem?

    Thank you very much

  2. #2
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,265
    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?

  3. #3
    Quote Originally Posted by tonton81 View Post
    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?
    Yes, exactly!
    100% of the items have to meet the condition

  4. #4
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,265
    Here you go buddy

    Code:
      uint8_t myArray[10] = { 10, 11, 2, 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] < 9) {
          fail_check = 1;
          break;
        }
      }
      ( fail_check ) ? Serial.println("FAIL") : Serial.println("PASS");

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •