-4

I have this assignment that requires me to turn a premise statement into code. The idea is that i need to print out the truth table for a premise statement. This is the premise: (((P v Q) ^ (Q -> R)) XOR (P v R)) <-> (R ^ Q)

I can create a manual Truth table Truth Table for the above premise

I just dont understand how to transform that into code? How would i approach that using the typical basic libraries such as iostream, string, math, etc. I can't utilize anything else like vectors, sets, etc.

You don't have to write literal code but perhaps some pseudocode might help or even justs tips.

I understand that "^" is &&, "v" is ||, "->" is "if-else" but i'm not sure about "<->" and "XOR" or simply how to put that in code.

Thanks in advance.

UPDATE: As per the assistance of my fellow stackoverflow peers, we've managed to obtain the literal meaning of each logical operator statement into a c++ approach.

(P v Q)   = P OR Q           = (P || Q)
(P ^ Q)   = P AND Q          = (P && Q)
(P XOR Q) = P ^ Q            = (P ^ Q)
(P -> Q)  = if P then Q      = (!P || Q)
(p <-> Q) = Only If P then Q = !(P ^ Q)

1 Answers1

1

You can (and should) use Boolean variables:

// declare variables and initialize.
bool p = true;
bool q = true;
bool r = true;
// Input values (from user or file)
//...

// Output some columns:
cout << p << " | " << q << " | " << r << " | ";
cout << (p || q) << " | ";
//...
cout << endl;
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • 2
    I think I'm missing something here. How does this address the OP's question? – templatetypedef Oct 12 '17 at 23:07
  • This shows an example of turning some logic operations into code. Also, it demonstrates outputting the first 4 columns of the example truth table. – Thomas Matthews Oct 12 '17 at 23:08
  • Thank You that makes sense. I'll try to do a single premise statement in boolean to test the waters. I'll post back with my example. – Jonathan Vazquez Oct 12 '17 at 23:11
  • @ThomasMatthews okay so i'm working on figuring out "if p then q" which is "p->q". I have this statement (!p || q). Which gives the right value in the truth table until i go to row 2. Which should be False not TRUE. – Jonathan Vazquez Oct 12 '17 at 23:26
  • @JonathanVazquez p -> q and !p or q are logically equivalent – RSon1234 Oct 12 '17 at 23:29
  • @RSon1234 your right idk why i though i was wrong. Now for "<->" which is "If not q then not p" || "if p then q". I tried using (!p && !q) which works for all rows in the truth table except for True which i get a returning 0. can this statements be really long? or they relatively small? – Jonathan Vazquez Oct 12 '17 at 23:39
  • @JonathanVazquez <-> is just XNOR. so it is just NOT(p XOR q). In C++ it would be !(p ^ q) – RSon1234 Oct 12 '17 at 23:44
  • @RSon1234 So i tried !(p || q) but the its not outputting a 1 for both T | T and F | F. Instead its outputting a 1 for F | T and T | F. – Jonathan Vazquez Oct 12 '17 at 23:49
  • @JonathanVazquez Edit your original question with the code you've written. More likely than not, there are some larger errors here... – scohe001 Oct 12 '17 at 23:50
  • @JonathanVazquez don't use ||. Use ^ as i said above – RSon1234 Oct 12 '17 at 23:51
  • @scohe001 i dont have code yet, i'm still trying to convert logical operators into their appropriate code. Once i have them all, i'll post them here so others can use them as reference. – Jonathan Vazquez Oct 12 '17 at 23:53
  • @RSon1234 apologies, idk why i wrote || in the comment above. Either way its doesnt work with "^". When i have !(0 && 0) my answer ends up been FALSE, when it should be TRUE. – Jonathan Vazquez Oct 12 '17 at 23:54
  • @JonathanVazquez now you are using &&? Stop confusing formal and c++. Literally type !(p ^ q). use the symbol ^ in your code. I am not saying and – RSon1234 Oct 12 '17 at 23:57
  • @scohe001 i thought the statement would of been !(0 && 0) -> !(1) since both sides of the "&&" are the same making the statement TRUE. and whats NOT(1), well its (0). – Jonathan Vazquez Oct 13 '17 at 00:00
  • @RSon1234 honestly i didn't even know that ^ was an operator to use. I just looked it up and its a bitwise operator, never used bitwise operators in my programming classes. This is a first, and its appreciated. I need to go back and learn those operators. Thanks, i think i have enough to go ahead and program my premise. – Jonathan Vazquez Oct 13 '17 at 00:02
  • @JonathanVazquez "since both sides...are the same" that would be true if we were talking about an XOR (`^` in c++), but you're using AND there... – scohe001 Oct 13 '17 at 00:03