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 returnfalse;
since the set of possible values of y includes 0.
#include <iostream>
#include <initializer_list>
usingnamespace 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 ) ) returnfalse;
returntrue;
}
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 ) )
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.
/*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 <iostream>
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
/*
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 <iostream>
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;
}
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.
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.
...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:
1 2 3 4 5
for (auto x : xs)
for (auto y : ys)
if (!Q( x, y ))
returnfalse;
returntrue;
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.