2

what I want to do is check an array of bools to see if 3 or more of them have been set to true. The only way I can think to do this is using a if statement for each possible combination of which there is lots because there are ten bools. Dose anybody have any suggestions on how best to do this.

1
  • Thanks Guys that helped had to edit the answer a bit because it was inside a while loop but other than that it worked perfectly Commented Jan 11, 2012 at 16:06

8 Answers 8

10

This would be the easiest way:

std::count(bool_array, std::end(bool_array), true) >= 3

Only problem is it keeps counting even after it has found 3. If that is a problem, then I would use sharptooth's method.

side note

I've decided to fashion an algorithm in the style of std::all_of/any_of/none_of for my personal library, perhaps you will find it useful:

template<typename InIt, typename P>
bool n_or_more_of(InIt first, InIt last, P p, unsigned n)
{
    while (n && first != last)
    {
        if (p(*first)) --n;
        ++first;
    }
    return n == 0;
}

For your purpose, you would use it like this:

n_or_more_of(bool_array, std::end(bool_array), [](bool b) { return b; }, 3);
Sign up to request clarification or add additional context in comments.

2 Comments

does this mean that I need to use using namespace std and if so can I still use my current using namespace tle.
@bobthemac: If you're going to use something from the standard library, then you either prefix it with std:: as I did above. Or you have a using declaration in your file.
8

The much easier way would be to loop through the array:

int numberOfSet = 0;
for( int i = 0; i < sizeOfArray; i++ ) {
     if( array[i] ) {
        numberOfSet++;
        //early cut-off so that you don't loop further without need
        // whether you need it depends on how typical it is to have
        // long arrays that have three or more elements set in the beginning
        if( numberOfSet >= 3 ) {
            break;
        }
     }
}

bool result = numberOfSet >= 3;

Comments

1

Whenever you are setting an array element into TRUE value, you can increment a global counter. This will be the simplest way. At any point in your code, the global array will tell you the number of TRUE elements in the Array.

Another thing - if you are keeping upto 32 bool values, you can use a single int variable. int is 32 bits (in Win32) and you can store 32 bool.

char x = 0; //  00000000 // char is 8 bits

// TO SET TRUE
x = x | (1 << 4); // 00010000
x = x | (1 << 7); // 10010000

// TO SET FALSE
x = x & ~(1 << 4); // 10010000 & 11101111 => 10000000

// TO CHECK True/False
if( x & ~(1 << 4) )

2 Comments

char? or CHAR? Either way, it's 8 bits. msdn.microsoft.com/en-us/library/aa505945.aspx
Yes, that's 8 bits, I was in a Java line, where char is 16 bits, to hold Unicode characters. Thanks for pointing out :-)
0

If it's an array, what you do is loop over it and count the number of trues. But I'm afraid you mean a bitpattern of some kind, right?

Comments

0

Why not just count the number of trues and then do something if the number is 3 or higher:

int sum = 0;
for (int i = 0; i < length; i++){
  if (arr[i]){
    sum++;
  }
}

if (sum >= 3){
  // do something...
}

Comments

0

You can loop through and build a bit-mask representation of the array, then you can compare against up to CHAR_BIT * sizeof (unsigned long) in parallel:

unsigned long mask = 0;
for (std::vector<bool>::const_iterator it = flags.begin(), end_it = flags.end();
     it != end_it;
     ++it)
{
  if (*it)
    mask |= (1 << (it - flags.begin()));
}

if (mask & (0xaa3)) // or whatever mask you want to check
{
}

This assumes that you're looking for patterns, not just want to count the number of true flags in the array.

Comments

0

Just loop through the array counting the number of bools set to true.

/**
 * @param arr The array of booleans to check.
 * @param n How many must be true for this function to return true.
 * @param len The length of arr.
 */
bool hasNTrue(bool *arr, int n, int len) {
    int boolCounter;
    for(int i=0; i<len; i++) {
        if (arr[i]) boolCounter++;
    }
    return boolCounter>=n;
}

Then call it like so

hasNTrue(myArray, 3, myArrayLength);

Comments

0

Store the bools as bits in an integer. Then apply one of the bit twiddling hacks.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.