Question

Hello, i'm making a C++ program that creates a truth table for these statements: 1. ?...

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;

}

Homework Answers

Answer #1

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:

  • Since you are dynamically generating truth table, you can not directly print the final result (valid or invalid) manually. This must be printed dynamically based on truth table. I have included that in code.

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!

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Create a memory diagram for the following C program for the second time the program reaches...
Create a memory diagram for the following C program for the second time the program reaches point one. int foo(const char *x, int y); int main(void) { int a, b; a=foo("abcde", 'p'); b=foo("purple", 't'); return 0; } int foo(const char *x, int y) { int c = -1, i = 0; while (x[i] != '\0') { if (x[i] == y) i++; } //point 1 return c; }
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath>...
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath> using namespace std; void divisors(int num); int main () {    char repeat;    int num;       while (repeat !='n')    {        cout << "Enter a number: ";        cin >> num;        divisors(num);        cout << "Continue? (y or n): ";        cin >> repeat;    }    return 0; } void divisors(int num) {   ...
1) Create a flowchart for the program below and Trace the program below with input of...
1) Create a flowchart for the program below and Trace the program below with input of 3 and then again with input of 5. #include <iostream> using namespace std; int Fall(int x, int m) { int Xm = 1, i=x; while(i>=x-m+1) { Xm = Xm*i; i=i-1; } return Xm; } int Delta(int x, int m) { int ans = 0; ans = Fall(x+1,m); ans = ans - Fall(x,m); return ans; } void main() { int x = 0, m =...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...
A.6 ... static int x = 1; int y = x * 2; void t1() {...
A.6 ... static int x = 1; int y = x * 2; void t1() {                 y++;                 cout << "x: " << x << " | y: " << y << endl;                 y += 1;                 x -= -1; } void t2() {                 int* x = &y;                 cout << "x: " << x << " | y: " << y << endl; } void t3() {                 int y = x;                 static int x...
C program fractions.c that does the following: 1. The program starts by making the user enter...
C program fractions.c that does the following: 1. The program starts by making the user enter a non-negative integer number a, called the first numerator. If the user enters a negative number, the program repeats the entry. 2. The program then makes the user enter a positive integer number b, called the first denominator. If the user enters a non-positive number, the program repeats the entry. 3. The program then makes the user enter a non-negative integer number c, called...
Question 1 Which statement is false about what Data Types defines Question 1 options: What values...
Question 1 Which statement is false about what Data Types defines Question 1 options: What values a variable cannot hold? How much memory will be reserved for the variable? What value a variable will hold? How the program will use the data type? Question 2 Using the structure below, which of the following statements about creating an array (size 20) of structures are not true? struct Employee{     string emp_id;     string emp_name;     string emp_sex; }; Question 2 options:...
C++ Fix my code This code is for imitating the round robin cpu scheduling algorithim using...
C++ Fix my code This code is for imitating the round robin cpu scheduling algorithim using linked lists. Currently I am able to input processes and store / display them properly. The issue begins somewhere after I have displayed the processlist (I get a segmentation fault (core dumped) or the code doesnt seem to run). I have marked the location of where I think the issue begins with a comment. Please fix the code so that it is working properly....
How to trace a c++ program by hand #include<iostream> using namespace std;    class Test {...
How to trace a c++ program by hand #include<iostream> using namespace std;    class Test {     int value; public:     Test(int v); };    Test::Test(int v) {     value = v; }    int main() {     Test t[100];     return 0; } _______________ #include <iostream> using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { for(j=1; j<=i; j++ ) { cout<<"*"; } cout << "\n";   } return 0; }
IN C++ - most of this is done it's just missing the bolded part... Write a...
IN C++ - most of this is done it's just missing the bolded part... Write a program that creates a class hierarchy for simple geometry. Start with a Point class to hold x and y values of a point. Overload the << operator to print point values, and the + and – operators to add and subtract point coordinates (Hint: keep x and y separate in the calculation). Create a pure abstract base class Shape, which will form the basis...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT