To program a function that tests whether all elements in an array are greater than 5
, the straightforward way will be:
bool test(std::vector<int> arr){
int number = 0;
for(auto i:arr){ // Iterate over all elements
if(i>5){
number++;
}
}
return arr.size() == number;
}
However, we can also achieve the function in this way:
bool test2(std::vector<int> arr){
for(auto i:arr){ // Iterate over all elements
if(i<=5){
return false;
}
}
return true;
}
Why is test2
functionally equivalent to test
? How to interpret them from a mathematical perspective?
test
do the things like ∀x ∈ arr, if x > 5, the result is true
while test2
do the things like ∃x ∈ arr, if x<=5, the result is false
. How do such two propositions mutually exclusive? Or, they are not propositions?