Lab Assignment # 2

 

 

Your program will accept as input the number of propositions that the user wishes to use.

(You can insist that this number be between 1 and 5, but place checks within your program.)

 

Then your program will print out the truth tables for the logical operators:  ,  and .

 

For example, if the use puts in 3 propositions your program should print out:

 

p1 |  p2  | p3  |ญญ_p1 and p2 and p3__

0   | 0     | 0    |  F

0   | 0     | 1    |  F

0   | 1     | 0    |  F

0   | 1     | 1    |  F

1   | 0     | 0    |  F

1   | 0     | 1    |  F

1   | 1     | 0    |  F

1   | 1     | 1    |  T

 

As well as the two other truth tables for the other two logical connectives.

 

Make sure that your output is in a nice format.

 

Each logical operator should be in a function of its own.  The main program should only deal with the input from the user, and call the functions.

 

Hint:  The lines above are successive Boolean numbers.  If you know the number of propositions, then you know the length of each of the Boolean numbers.  Here is a short program (courtesy of M. Tausner) that prints out the right hand sides of a truth table.  You may use as much of it as you need.

 

// a simple modular program 
// generates the left side of a truth table
#include <iostream>
#include <math.h>
using namespace std;
 
#define SIZE 4
 
void printary (int[], int);     // prints an array
void nextb (int[], int);               // returns the next binary number
 
int main () 
{
        int j, binary[SIZE] = {0};      // initialize array binary to all zeros
        int N = (int)pow(2,SIZE);       // requires header file math.h  
        for (j=1;j<=N;j++)
        {
               printary(binary,SIZE); cout << endl;
               nextb(binary,SIZE);
        }
        return 0;
}
 
// prints an integer array A given its size n
void printary(int A[], int n)
{
        for (int k=0;k<n; k++)
               cout << "  " << A[k];
}
 
// assumes the integer array B contains a binary number
// returns the next binary number
void nextb(int B[], int n)
{
        for (int k=n-1;k>=0; k--)         // scan from right to left 
               if (B[k] == 0){                   // find rightmost 0
                       B[k]=1;           // change it to 1   
                       return;           // and return 
               }
               else
                       B[k]=0;           // otherwise change 1 to 0
}