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;
}