Thread: Check all values in bool array so that they are true without using return, break?

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171

    Question Check all values in bool array so that they are true without using return, break?

    So I have found very many articles about how you can do this, but all of the solutions that I've found are using multiple return statements. And that is not allowed in this case. It is not allowed to use break to break out of a loop either. So how can you accomplish this without using multiple return statements or break?

    I am allowed to use one return statement at the end of the function.

    So this is not working because I'm only checking if 1 value is true and then I move on to check if the next is true. So if only 1 value is true it will set hasWon = true;
    Code:
    bool hasWon = false;
    const int size = 10;
    bool arr[size] = {0};
    
    for (int i = 0; i < size; i++) {
        if (arr[i] == true) {
             hasWon = true;
        }
    }
    Do I need a nested for-loop for this to work so that I check for all values that are true somehow?

    Research links:

    How do I check if all booleans in an array are true? - Unity Answers

    java - What is the most elegant way to check if all values in a boolean array are true? - Stack Overflow
    Last edited by DecoratorFawn82; 10-04-2018 at 12:50 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I would do
    Code:
    for (int i = 0; !hasWon && i < size; i++)
    Also, it's only necessary to do
    Code:
    if (arr[i] )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171
    Sure, this would break out of the loop.
    But does it really check so that every value in the array is true?

    I mean it does, but if one value is true it sets hasWon = true and
    breaks out of the loop leaving the other elements as 0 and still
    says the player has won?

    Am I wrong now?
    Last edited by DecoratorFawn82; 10-04-2018 at 02:11 PM. Reason: grammar fix

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    If you want to know if all the values are true then it is probably easiest just to iterate through the array and count how many elements are true. If the counter is equal to the number of elements then all are true.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You may also want to consider using something like a std::bitset or an unsigned integer type that has a sufficient number of bits and do bit twiddling to determine the status of the individual bits.

  6. #6
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171
    Quote Originally Posted by jimblumberg View Post
    If you want to know if all the values are true then it is probably easiest just to iterate through the array and count how many elements are true. If the counter is equal to the number of elements then all are true.
    Yeah, that will work Thank you!

  7. #7
    Registered User
    Join Date
    May 2015
    Posts
    56
    Code:
    //bool hasWon = false;
    const int size = 10;
    bool arr[size] = {0};
     
    int count = 0;
    for (int i = 0; i < size; i++) {
            if (arr[i]) {
                 count += 1;
            }
    }
    //count is the total number of trues.
    //this is what people have suggested.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That algorithm jimblumberg suggested is correct, but the thing is that it is also inefficient: the loop must always run for size number of iterations, even if the very first element of the array was false.

    You might recall from studying predicate logic as part of your computer science curriculum that in order to prove a universally quantified statement false, you just need to find a counterexample. Hence, we might write:
    Code:
    bool allTrue(bool values[], int size) {
        for (int i = 0; i < size; ++i) {
            if (!values[i]) {
                return false;
            }
        }
        return true;
    }
    So what is happening here is that we run a loop to find a counterexample. If one is found, great. We know that the statement "for all values x, x is true" is false. If not, well, since we have exhausted the possibility of finding a counterexample, it follows that indeed the statement is true, so we return true. You probably saw variations of this many times when searching the Web for examples, and naturally so since it expresses the maths quite nicely.

    Unfortunately, it seems you have an artificial restriction on multiple return statements (this might be inspired by something called "single entry - single exit", but that idea is not useful in C++ since C++ has exceptions as a native feature). It also seems to have an artificial restriction on break statements (this might be inspired by them being akin to goto, but in fact break statements are not so arbitrary, hence they don't suffer from quite the same problems as goto).

    What you can do though, is to make this mathematical assertion about the universally quantified statement through your hasWon variable, i.e., you assert that the statement is true (hasWon = true), then attempt to find a counterexample as long as the assertion remains true:
    Code:
    bool allTrue(bool values[], int size) {
        bool hasWon = true;
        for (int i = 0; i < size && hasWon; ++i) {
            if (!values[i]) {
                hasWon = false;
            }
        }
        return hasWon;
    }
    Last edited by laserlight; 10-04-2018 at 04:11 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A function that returns a bool if true.
    By MCDD in forum C++ Programming
    Replies: 2
    Last Post: 11-02-2012, 03:58 PM
  2. bool function always seems to return true
    By The Doctor in forum C++ Programming
    Replies: 1
    Last Post: 10-22-2012, 11:07 PM
  3. How to return certain values from an array?
    By Valandu in forum C Programming
    Replies: 1
    Last Post: 11-06-2011, 11:25 PM
  4. Replies: 7
    Last Post: 10-05-2011, 03:21 PM
  5. Replies: 4
    Last Post: 10-03-2011, 06:30 AM

Tags for this Thread