Hello, i'm making a C++ program that creates a truth table for these statements:
1. ? ↔ ? ≡ ((? ∨ ?) ∧ (¬? ∨ ¬?))
2. ¬(? ∧ ?) ≡ ¬? ∨ ¬?
3. ? ↔ ? ≡ ((? → ?) ∧ (? → ?))
4. (? → ?) → ? ≡ ? → (? → ?)
I cannot use C++ standard library, and i'll be implementing the program using a linux command line.
for the math symbols i'll using the following:
? P M P M P
∧ * ∨ + ¬ ~
⨁ ^ → -> ↔ ≡
= ==
Here's a sample output:
Sample Output:
Equivalence: p->q === ~p+q
p q p->q <-> ~p+q
0 0 1 1 1
0 1 1 1 1
1 0 0 1 0
1 1 1 1 1
p->q is equivalent to ~p+q
Here's the program I have so far, i'm not sure sure if its entirely correct. Please make any corrections and/or comments if needed Thank you!
#include<iostream>
using namespace std;
void method1(int x,int y)
{
// p <-> q == ((p v q) ^ (~pv~q))
cout<<"\n\n"<<x<<"\t"<<y<<"\t"<<((x==y)?"1":"0")<<"\t\t"<<((x|y)&(!x|!y));
}
void method2(int x,int y)
{
// ~(p^q) == ~p v ~q
cout<<"\n\n"<<x<<"\t"<<y<<"\t"<<!(x&y)<<"\t\t"<<(!x|!y);
}
void method3(int x,int y)
{
//p <-> q == ((p->q ^ (q->p)))
cout<<"\n\n"<<x<<"\t"<<y<<"\t"<<((x==y)?"1":"0")<<"\t\t"<<(((x==1)&&(y==0)?0:1)&((x==0)&&(y==1))?1:0);
}
void method4(int x,int y, int z)
{
// (p->q)-> r == p ->(q->r)
cout<<"\n\n"<<x<<"\t"<<y<<"\t"<<
(( (x==1) && (y==0) ?0:1) && (z==1)?1:0)<<"\t\t"<<((x==1)&&((y==1)&&(z==0)?1:0)?1:0);
}
int main()
{
int x,y,z;
cout<<"p\tq\t p<->q \t ((p v q) ^ (!p v !q)) \t";
for(x=0;x<=1;++x)
for(y=0;y<=1;++y)
{
method1(x,y);
}
cout<<"\nTable for Question 1: It is not valid as LHS not equal to RHS.\n";
cout<<"\np\tq \t!(p ^ q) \t (!p v !q) \t";
for(x=0;x<=1;++x)
for(y=0;y<=1;++y)
{
method2(x,y);
}
cout<<"\nTable for Question 2: It is valid as LHS is equal to RHS.\n";
cout<<"\np\tq\t (p <-> q) \t ((p->q) ^ (q->p)) \t";
for(x=0;x<=1;++x)
for(y=0;y<=1;++y)
{
method3(x,y);
}
cout<<"\nTable for Question 3: It is not valid as LHS not equal to RHS.\n";
cout<<"\np\tq\t (p -> q) -> r \t ((p->(q ->r)) \t";
for(x=0;x<=1;++x)
for(y=0;y<=1;++y)
for(z = 0; z<=1; ++z)
{
method4(x,y, z);
}
cout<<"\nTable for Question 4: It is not valid as LHS not equal to RHS.\n";
return 0;
}
The code seems to be almost correct. However, there are few things that are wrong.
1. method3() RHS calculation is wrong
Given : ((? → ?) ∧ (? → ?))
Your calculation: (((x==1)&&(y==0)?0:1)&((x==0)&&(y==1))?1:0)
You have mistakenly interchanged 1 and 0 at the very end of expression
Instead of doing it this way, you can do this:
Simplified expression : ((!? | ?) ∧ (!? | ?))
Therefore, question 3 is a valid expression (your ans was invalid)
There's some other thing I would like to point:
CODE:
#include<iostream>
using namespace std;
bool method1(int x, int y)
{
// p <-> q == ((p v q) ^ (~pv~q))
int lhs = int (x == y);
int rhs = int ((x | y) & (!x | !y)); // this is
equivalent to x != y
cout << x << "\t" << y <<
"\t" << lhs << "\t\t" << rhs <<
"\n\n";
return lhs == rhs;
}
bool method2(int x, int y)
{
// ~(p^q) == ~p v ~q\
int lhs = !(x & y);
int rhs = (!x | !y);
cout << x << "\t" << y << "\t" << lhs << "\t\t" << rhs << "\n\n";
return lhs == rhs;
}
bool method3(int x, int y)
{
//p <-> q == ((p->q ^ (q->p)))
int lhs = int (x == y);
int rhs = (!x | y) & (!y | x); // since p->q ==
~p v q == ~p & q
cout << x << "\t" << y << "\t" << lhs << "\t\t" << rhs << "\n\n";
return lhs == rhs;
}
bool method4(int x, int y, int z)
{
// (p->q)-> r == p ->(q->r)
int lhs = (!(!x | y) | z);
int rhs = (!x | (!y | z));
cout << x << "\t" << y << "\t" << lhs << "\t\t" << rhs << "\n\n";
return lhs == rhs;
}
int main()
{
int x, y, z;
bool isValid;
string validStr = "It is valid as LHS equals to
RHS";
string invalidStr = "It is not valid as LHS not equal
to RHS";
cout << "Question 1
===================\n";
cout <<
"p"<<"\t"<<"q"<<"\t"<<"p<->q"<<"\t"<<"((p
v q) ^ (!p v !q))"<<"\n\n";
isValid = true;
for (x = 0; x <= 1; ++x)
for (y = 0; y <= 1; ++y)
if (!method1(x,
y))
isValid = false;
cout << (isValid ? validStr : invalidStr) << "\n\n";
cout << "Question 2
===================\n";
cout <<
"p"<<"\t"<<"q"<<"\t"<<"!(p ^
q)"<<"\t"<<"(!p v !q)"<<"\n\n";
isValid = true;
for (x = 0; x <= 1; ++x)
for (y = 0; y <= 1; ++y)
if (!method2(x,
y))
isValid = false;
cout << (isValid ? validStr : invalidStr) << "\n\n";
cout << "Question 3
===================\n";
cout <<
"p"<<"\t"<<"q"<<"\t"<<"(p <->
q)"<<"\t"<<"((p->q) ^ (q->p))"<<"\n\n";
isValid = true;
for (x = 0; x <= 1; ++x)
for (y = 0; y <= 1; ++y)
if (!method3(x,
y))
isValid = false;
cout << (isValid ? validStr : invalidStr) << "\n\n";
cout << "Question 4
===================\n";
cout <<
"p"<<"\t"<<"q"<<"\t"<<"(p -> q) ->
r"<<"\t"<<"(p->(q ->r)"<<"\n\n";
isValid = true;
for (x = 0; x <= 1; ++x)
for (y = 0; y <= 1; ++y)
for (z = 0; z <= 1; ++z)
if (!method4(x, y, z))
isValid = false;
cout << (isValid ? validStr : invalidStr) << "\n\n";
return 0;
}
Screenshot:
Best wishes!
Get Answers For Free
Most questions answered within 1 hours.