Let Q(x, y) denote xy< 0
The domain of x is {1,2,3} and the domain of y is {0, -1, -2, -3}.
Implement the code that outputs the truth values of the following quantifications:
∀x ∀y Q(x, y)

**Display values of variables x and y if the quantification is true.
To see if∀x∀yP(x,y)is true, loop through the values of x: 
At each step, loop through the values for y.
If for some pair of x and y,P(x,y)is false, then ∀x∀yP(x,y)is false and both the outer and inner loop terminate.

∀x∀yP(x,y)is true if the outer loop ends after stepping through each x.
I think you had better explain your question better (or maybe it's because I didn't read Computer Science).

 Implement the code that outputs the truth values of the following quantifications: ∀x ∀y Q(x, y)

As far as I can see that amounts to
"for all x, for all y, xy < 0"
which appears to be answerable by
`return false;`
since the set of possible values of y includes 0.

I guess you could do
 ``1234567891011121314151617181920`` ``````#include #include using namespace std; template< typename T > bool proposition( const T &first, const T &second, bool test( int, int ) ) { for ( auto x : first ) for ( auto y : second ) if ( !test( x, y ) ) return false; return true; } int main() { auto x = { 1, 2, 3 }, y = { 0, -1, -2, -3 }, z = { -5, -6, -7 }; auto Q = []( int i, int j ){ return i * j < 0; }; cout << boolalpha << proposition( x, y, Q ) << '\n'; cout << boolalpha << proposition( x, z, Q ) << '\n'; }``````

 ```false true```

Or in Python:
 ```def proposition( first, second, test ): for x in first: for y in second: if not test( x, y ): return False return True x, y, z = { 1, 2, 3 }, { 0, -1, -2, -3 }, { -5, -6, -7 } Q = lambda i, j : i * j < 0 print( proposition( x, y, Q ) ) print( proposition( x, z, Q ) )```
Last edited on
The given homework has a problem: 0 ∈ y, so all outputs will be false, which is the most boring version of output you can get.

Did you get multiple sets for x and y?
I think you ought to try both with and without that zero in there.
(Also try some that have a negative value for some y, or a positive value for some x.)

 **Display values of variables x and y if the quantification is true.

That is ambiguous. Ask for clarification.

@lastchance’s answer should help you some. He made a function that does exactly what your homework prompt asked: except he did it super-mega-special version, using a lambda. You don’t need that. Just create a separate function named “Q” and have the “P” function call it directly. (Also, he didn’t print any xs or ys for the true result.)

You could also use arrays for your xs and ys, but the use of the std::initializer_list was very cool; unless you are in a basic C++ 101 type course, feel free to totally do that.
Last edited on
 ``1234567891011121314151617181920212223242526272829303132333435`` ``````/*To see if∀x∀yP(x,y)is true, loop through the values of x:  At each step, loop through the values for y. If for some pair of x and y,P(x,y)is false, then ∀x∀yP(x,y)is false and both the outer and inner loop terminate. ∀x∀yP(x,y)is true if the outer loop ends after stepping through each x. */ #include int main() { int x[]{1,2,3}; int y[]{0, -1, -2, -3}; int P[3][4]; for(int i = 0; i < 3; i++) { for(int j = 0; j < 4; j++) { P[i][j] = x[i] * y[j]; if( P[i][j] >= 0 ) { std::cout << "The proposition P( Q(x,y) >= 0, is true for all x, y) is false\n"; exit(99); } } std::cout << '\n'; } return 0; } ``````

 ```The proposition P( Q(x,y) >= 0, is true for all x, y) is false Program ended with exit code: 99```
Last edited on
 ``123456789101112131415161718192021222324252627282930313233343536`` ``````/* Let Q(x, y) denote xy< 0 The domain of x is {1,2,3} and the domain of y is {0, -1, -2, -3}. Implement the code that outputs the truth values of the following quantifications: ∀x ∀y Q(x, y) **Display values of variables x and y if the quantification is true. */ #include int main() { int x[]{1,2,3}; int y[]{0, -1, -2, -3}; int P[3][4]; for(int i = 0; i < 3; i++) { for(int j = 0; j < 4; j++) { P[i][j] = x[i] * y[j]; if( P[i][j] < 0 ) { std::cout << x[i] << '*' << y[j] << " = " << P[i][j] << '\t' << "True\n"; } } } return 0; } ``````

 ```1*-1 = -1 True 1*-2 = -2 True 1*-3 = -3 True 2*-1 = -2 True 2*-2 = -4 True 2*-3 = -6 True 3*-1 = -3 True 3*-2 = -6 True 3*-3 = -9 True Program ended with exit code: 0```
dina1 wrote:
**Display values of variables x and y if the quantification is true.

In the first version by againtry one should probably do this "display" (whatever it means) on line 32. (After the loops, before the `return 0;`)
That depends on whether the "**Display" line is associated with the first part of @OP's quote or the second part. All told it again demonstrates more ambiguity in the question. I decided the "** Display" line has nothing to do (even as a reference to a footnote) with the original second part. So I deliberately put the line where I decided to put it not where somebody else wanted to.
dina1 wrote:
∀x ∀y Q(x, y)

What does this mean? I've never seen an upside-down A before, except when I accidentally pick up a book upside down ;)
 The upside-down A symbol is the universal quantifier from predicate logic. (Also see the more complete discussion of the first-order predicate calculus.) As others noted, it means that the stated assertions holds "for all instances" of the given variable (here, s). You'll soon run into its sibling, the backwards capital E, which is the existential quantifier, meaning "there exists at least one" of the given variable conforming to the related assertion.

https://stackoverflow.com/questions/1925979/unfamiliar-symbol-in-algorithm-what-does-%E2%88%80-mean
...or read aloud: “for all x and for all y apply Q of x, y”
...which translates to classical computer languages as a nested loop with a function call in the innermost loop, as you have seen already here:

 ``12345`` ``````for (auto x : xs) for (auto y : ys) if (!Q( x, y )) return false; return true;``````

Also of note: the entire (mathematical) expression is itself a predicate with truthy value of true only if Q(x, y) holds for all x and all y.
Gurgh. I suck at math, which is probably why I'm only an electrician.

 The upside-down A symbol is the universal quantifier from predicate logic. (Also see the more complete discussion of the first-order predicate calculus.)

I read that three times, and each time it just sailed right over my head.

But thanks for trying to explain it!
Just read it as "for all" - I did!

It goes admirably with C++'s range-based for loops.
Last edited on
Topic archived. No new replies allowed.