Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C++(using Visual Studio): Write a program that produces the truth table for t

ID: 3877695 • Letter: I

Question

In C++(using Visual Studio): Write a program that produces the truth table for the following compound propositions. Write the header(s) of the table(s), including intermediate steps and the final result. Output the result on the file_prog2 output.txt.

(d) (p q) (neg q neg p)

(e) (p or q) and neg (p and q)

(f) p and neg p

The table should contain the letters 'T' and 'F', it should not print 1s and 0s. For the conditional (->) and biconditional (<->), you can use the logical equivalences.

Explanation / Answer

#include<iostream>
#include<fstream>
using namespace std;
bool* implication(bool p[],bool q[])
{
   bool *r= new bool[4];
   int i;
   for(i=0;i<4;i++)
   if(p[i]==true )//finding truth values
   {
       if(q[i]==false)
       r[i]=false;
       else
       r[i]=true;
   }
   else r[i]=true;
   return r;//returning result
}
bool* Or(bool p[],bool q[])
{
   bool *r= new bool[4];
   int i;
   for(i=0;i<4;i++)
   if(p[i]==true )//finding truth values
   r[i]=true;
   else if(q[i]==true)
   r[i]=true;
   else r[i]=false;
   return r;//returning result
}
bool* And(bool p[],bool q[])
{
   bool *r= new bool[4];
   int i;
   for(i=0;i<32;i++)
   if(p[i]==false )//finding truth values
   r[i]=false;
   else if(q[i]==false)
   r[i]=false;
   else r[i]=true;
   return r;//returning result
}
bool* biconditional(bool p[],bool q[])
{
   bool *r= new bool[4];
   int i;
   for(i=0;i<4;i++)
   if(p[i]==true )//finding truth values
   {   if(q[i]==true)
           r[i]=true;
       else r[i]=false;
   }
   else
   {
       if(q[i]==false)
       r[i]=true;
       else r[i]=false;
      
   }
  
   return r;//returning result
}
bool* negation(bool p[])
{
   bool *r= new bool[4];
   int i;
   for(i=0;i<4;i++)
   if(p[i]==true )//finding truth values
   {
       r[i]=false;
   }
   else r[i]=true;
   return r;//returning result
}

int main()
{
  
   bool p[4]={true,true,false,false},q[4]={true,false,true,false};
   bool *p_imp_q,*p_and_q,*p_or_q,*n_p,*n_q,*nq_imp_np,*np_and_q;
   p_imp_q = implication(p,q);
   n_p = negation(p);
   n_q = negation(q);
   nq_imp_np = implication(n_q,n_p);
   p_or_q = Or(p,q);
   p_and_q = And(p,q);
   np_and_q = negation(p_and_q);
   //final results;
   bool *d,*e,*f;
   d = biconditional(p_imp_q,nq_imp_np);
   e = And(p_or_q,np_and_q);
   f = And(p,n_p);
  
  
   //printing
   ofstream file;
   file.open("file_prog2_output.txt");
  
  
   //writing d
   cout<<"--------------------Truth table(d)-------------------- ";
   file<<"--------------------Truth table(d)-------------------- ";
   cout<<" P | Q | ~P | ~Q | P -> Q | ~Q->~P | (P -> Q)<->(~Q->~P) ";
   file<<" P | Q | ~P | ~Q | P -> Q | ~Q->~P | (P -> Q)<->(~Q->~P) ";
  
   for(int i=0;i<4;i++)
   {
       if(p[i]==true)
       {cout<<" T |";
       file<<" T |";
       }
       else {
       cout<<" F |";
       file<<" F |";
       }
       if(q[i]==true)
       {cout<<" T |";
       file<<" T |";
       }
       else {
       cout<<" F |";
       file<<" F |";
       }
       if(n_p[i]==true)
       {cout<<" T |";
       file<<" T |";
       }
       else {
       cout<<" F |";
       file<<" F |";
       }
       if(n_q[i]==true)
       {cout<<" T |";
       file<<" T |";
       }
       else {
       cout<<" F |";
       file<<" F |";
       }
       if(p_imp_q[i]==true)
       {cout<<" T |";
       file<<" T |";
       }
       else {
       cout<<" F |";
       file<<" F |";
       }
       if(nq_imp_np[i]==true)
       {cout<<" T |";
       file<<" T |";
       }
       else {
       cout<<" F |";
       file<<" F |";
       }
       if(d[i]==true)
       {cout<<" T ";
       file<<" T ";
       }
       else {
       cout<<" F ";
       file<<" F ";
       }
      
          
   }
  
  
   //writing e
   cout<<"--------------------Truth table(e)-------------------- ";
   file<<"--------------------Truth table(e)-------------------- ";
   cout<<" P | Q | P or Q | P and Q | ~(P and Q) | (P or Q)and ~(P and Q) ";
   file<<" P | Q | P or Q | P and Q | ~(P and Q) | (P or Q)and ~(P and Q) ";
  
  
for(int i=0;i<4;i++)
   {
       if(p[i]==true)
       {cout<<" T |";
       file<<" T |";
       }
       else {
       cout<<" F |";
       file<<" F |";
       }
       if(q[i]==true)
       {cout<<" T |";
       file<<" T |";
       }
       else {
       cout<<" F |";
       file<<" F |";
       }
       if(p_or_q[i]==true)
       {cout<<" T |";
       file<<" T |";
       }
       else {
       cout<<" F |";
       file<<" F |";
       }
       if(p_and_q[i]==true)
       {cout<<" T |";
       file<<" T |";
       }
       else {
       cout<<" F |";
       file<<" F |";
       }
       if(np_and_q[i]==true)
       {cout<<" T |";
       file<<" T |";
       }
       else {
       cout<<" F |";
       file<<" F |";
       }
       if(e[i]==true)
       {cout<<" T ";
       file<<" T ";
       }
       else {
       cout<<" F ";
       file<<" F ";
       }
  
   }


   //writing f
   cout<<"--------------------Truth table(f)-------------------- ";
   file<<"--------------------Truth table(f)-------------------- ";
   cout<<" P | Q | ~P | P and ~P ";
   file<<" P | Q | ~P | P and ~P ";
  
  
for(int i=0;i<4;i++)
   {
       if(p[i]==true)
       {cout<<" T |";
       file<<" T |";
       }
       else {
       cout<<" F |";
       file<<" F |";
       }
       if(q[i]==true)
       {cout<<" T |";
       file<<" T |";
       }
       else {
       cout<<" F |";
       file<<" F |";
       }
       if(n_p[i]==true)
       {cout<<" T |";
       file<<" T |";
       }
       else {
       cout<<" F |";
       file<<" F |";
       }
       if(f[i]==true)
       {cout<<" T |";
       file<<" T |";
       }
       else {
       cout<<" F ";
       file<<" F ";
       }


}
   return 0;
}

output:

--------------------Truth table(d)--------------------
P | Q | ~P | ~Q | P -> Q | ~Q->~P | (P -> Q)<->(~Q->~P)
T | T | F | F | T | T | T
T | F | F | T | F | F | T
F | T | T | F | T | T | T
F | F | T | T | T | T | T
--------------------Truth table(e)--------------------
P | Q | P or Q | P and Q | ~(P and Q) | (P or Q)and ~(P and Q)
T | T | T | T | F | F
T | F | T | F | T | T
F | T | T | F | T | T
F | F | F | F | T | F
--------------------Truth table(f)--------------------
P | Q | ~P | P and ~P
T | T | F | F
T | F | F | F
F | T | T | F
F | F | T | F