# Thread: way to check 3 elements of array and set 4th

1. ## way to check 3 elements of array and set 4th

I'm trying to think of how I can check 3 elements of a 4 element array and set the 4th. I have a bool array of 4 elements that all start off false. If any 3 of the elements are true, the other element needs to be set to true also. If only 1 or 2 elements are true, nothing is changed.

2. That would be rather simple, methinks. What code have you written so far in your attempt to solve your problem?

EDIT:
If any 3 of the elements are true, the other element needs to be set to true also. If only 1 or 2 elements are true, nothing is changed.
Surely you mean "if all 3 of the other elements are true", otherwise you have conflicting requirements, or?

3. Code:
```   bool arr[4];
int nrs = 0, ns;
for ( int idx = 0; idx < 4; idx ++ ) {
if ( arr[idx] ) nrs ++;
else ns = idx;
}
if ( nrs == 3 ) arr[ns]=true;```
Don't know if that is what you want.
this checks if any 3 of the 4 are set if yes sets the 4th as well
Kurt

4. Thanks Zuk, that looks like exactly what i was wanting. I was over thinking the simplicity of it