Question

In: Computer Science

Write a program that generates and prints out in a neat format all possible truth tables...

Write a program that generates and prints out in a neat format all possible truth tables with three propositional variables p, q, and r. Your program should also number generated truth tables and output the numbers so it is clear how many total tables were generated.

Your program output may look like the following (Note: only the beginning of the output is shown):

Truth table 1:
p q r proposition

-----------------

F F F F
F F T F
F T F F
F T T F
T F F F
T F T F
T T F F
T T T F

Truth table 2:
p q r proposition

-----------------

F F F F
F F T F
F T F F
F T T F
T F F F
T F T F
T T F F
T T T T

Implementation details:

  •  All possible truth tables must be automatically generated by your program. That is, the last column values must be automatically generated (e.g., use eight nested loops to do it). The first three columns of truth tables could be hard-coded in your program.

  •  All truth tables must be numbered in the output.

  •  Printed truth tables must have ‘T’s and ‘F’s in them (do not use ‘1’ and ‘0’).

##### CURRENT CODE SO FAR, ONLY NEED THE IF STATEMENTS TO OUTPUT THE 4TH COLUMN######

#include<stdio.h>

int main() {
  
       int row1, row2, row3, row4, row5, row6, row7, row8, counter=1, truth_table;
       char array[8][20]={
               "F F F",
               "F F T",
               "F T F",
               "F T T",
               "T F F",
               "T F T",
               "T T F",
               "T T T",
       };

       for(row1 = 0; row1 < 2; row1++){
               for(row2 = 0; row2 < 2; row2++){
                       for(row3 = 0; row3 < 2; row3++){
                               for(row4 = 0; row4 < 2; row4++){
                                       for(row5 = 0; row5 < 2; row5++){
                                               for(row6 = 0; row6 < 2; row6++){
                                                       for(row7 = 0; row7 < 2; row7++){
                                                               for(row8 = 0; row8 < 2; row8++){
                                                          
                                                                       printf("\nTruth Table %d\n", counter);
                                                                       printf("p q r proposition\n");
                                                                       printf("-----------------\n");
                                                                       for(truth_table = 0; truth_table < 8; truth_table++){
                                                                               printf("%s\n", array[truth_table]);
                                                                       }  
                                                                       counter++;
                                                                      
                                                               };
                                                              
                                                       };
                                               };
                                       };
                               };
                       };
               };
       };
}

Solutions

Expert Solution

We do not want to put the printing of the specific rows in a for loop, since we do not want it to be generalized. Let us split it into each row. For every row, the value of the last proposition will depend on the variable for that row.

The code can be modified as follows:

#include<stdio.h>

int main() {
  
       int row1, row2, row3, row4, row5, row6, row7, row8, counter=1, truth_table;
       char array[8][20]={
               "F F F",
               "F F T",
               "F T F",
               "F T T",
               "T F F",
               "T F T",
               "T T F",
               "T T T",
       };

       for(row1 = 0; row1 < 2; row1++){
               for(row2 = 0; row2 < 2; row2++){
                       for(row3 = 0; row3 < 2; row3++){
                               for(row4 = 0; row4 < 2; row4++){
                                       for(row5 = 0; row5 < 2; row5++){
                                               for(row6 = 0; row6 < 2; row6++){
                                                       for(row7 = 0; row7 < 2; row7++){
                                                               for(row8 = 0; row8 < 2; row8++){
                                                          
                                                                       printf("\nTruth Table %d\n", counter);
                                                                       printf("p q r proposition\n");
                                                                       printf("-----------------\n");

                                                                       printf("%s %c\n", array[0], (row1==0)?'F':'T');

                                                                       printf("%s %c\n", array[1], (row2==0)?'F':'T');

                                                                       printf("%s %c\n", array[2], (row3==0)?'F':'T');

                                                                       printf("%s %c\n", array[3], (row4==0)?'F':'T');

                                                                       printf("%s %c\n", array[4], (row5==0)?'F':'T');

                                                                       printf("%s %c\n", array[5], (row6==0)?'F':'T');

                                                                       printf("%s %c\n", array[6], (row7==0)?'F':'T');

                                                                       printf("%s %c\n", array[7], (row8==0)?'F':'T');
                                                                       counter++;
                                                                      
                                                               };
                                                              
                                                       };
                                               };
                                       };
                               };
                       };
               };
       };
}

An easier way would be to put the row variables in an array of integers too, so that another loop can do what is needed.

#include<stdio.h>

int main() {

       int row[8], counter=1, truth_table;
       char array[8][20]={
               "F F F",
               "F F T",
               "F T F",
               "F T T",
               "T F F",
               "T F T",
               "T T F",
               "T T T",
       };


       for(row[0] = 0; row[0] < 2; row[0]++){
               for(row[1] = 0; row[1] < 2; row[1]++){
                       for(row[2] = 0; row[2] < 2; row[2]++){
                               for(row[3] = 0; row[3] < 2; row[3]++){
                                       for(row[4] = 0; row[4] < 2; row[4]++){
                                               for(row[5] = 0; row[5] < 2; row[5]++){
                                                       for(row[6] = 0; row[6] < 2; row[6]++){
                                                               for(row[7] = 0; row[7] < 2; row[7]++){
                                                        
                                                                       printf("\nTruth Table %d\n", counter);
                                                                       printf("p q r proposition\n");
                                                                       printf("-----------------\n");
                                                                       for (truth_table=0; truth_table<8; truth_table++)
                                                                          printf("%s %c\n", array[truth_table], row[truth_table]==0?'F':'T');
                                                                       counter++;
                                                                    
                                                               };
                                                            
                                                       };
                                               };
                                       };
                               };
                       };
               };
       };
}


Related Solutions

Create a program that generates a file of random numbers, and then prints them in neat...
Create a program that generates a file of random numbers, and then prints them in neat fashion to another file, and also saves to that file the average and standard deviation of those numbers. I) First, you would need to generate a file of random numbers that consists of N random numbers (100 < N < 1000). Each random digit should be a real number (type double) between 0 and 50. This file and its digits would now serve as...
Write a program that asks the user for an integer and then prints out all its...
Write a program that asks the user for an integer and then prints out all its factors. For example, when the user enters 84, the program should print 2 2 3 7 Validate the input to make sure that it is not a character or a string using do loop.in c++
Write a C++ program that prints out all of the command line arguments passed to the...
Write a C++ program that prints out all of the command line arguments passed to the program. Each command line argument should be separated from the others with a comma and a space. If a command line argument ends in a comma, then another comma should NOT be added
Write a program that prints out all numbers, less than or equal to 10,000,000, that are...
Write a program that prints out all numbers, less than or equal to 10,000,000, that are evenly divisible by all numbers between 5 and 15 (inclusive). Do not use any libraries besides stdio.h. Need it in 10 minutes, please.
C++: Write a program that produces truth tables for the following compound propositions. Write the header...
C++: Write a program that produces truth tables for the following compound propositions. Write the header of the tables, including intermedial steps and the final result. There should be three tables. Output the result on the file prog2 output.txt. 1. p&!(p|q) 2. (p|q)&!(p&q) 3. (p–>q)<->(!q–>!p) NOTE: Do not hard code truth tables. The program must use binary or bitwase operators to compute the results.
Use Python to solve: show all work 1) Write a program that prints out a deck...
Use Python to solve: show all work 1) Write a program that prints out a deck of cards, in the format specified below. (That is, the following should be the output of your code). 2 of hearts 2 of diamonds 2 of spades 2 of clubs 3 of hearts 3 of diamonds 3 of spades 3 of clubs 4 of hearts 4 of diamonds 4 of spades 4 of clubs Continue with 5,6,7.. ending with A of hearts A of...
Write a program that prints out the characters of a string each on a line and...
Write a program that prints out the characters of a string each on a line and counts these characters.  (Do not use the length function len() ).
(C++) Write a program that prints a table in the following format given a "x" read...
(C++) Write a program that prints a table in the following format given a "x" read from the keyboard. For example, if x is 4, it prints out 0 0 1 1 2 4 3 9 4 16 To avoid mismatch, put 8 white spaces between two numbers.
Write a program which prompts the user for a positive integer, and then prints out the...
Write a program which prompts the user for a positive integer, and then prints out the prime factorization of their response. Do not import anything other than the Scanner. One way you might go about this using nested loops: Start a "factor" variable at 2 In a loop: repeatedly print the current factor, and divide the user input by it, until the user input is no longer divisible by the factor increment the factor This plan is by no stretch...
In Python write a program that calculates and prints out bills of the city water company....
In Python write a program that calculates and prints out bills of the city water company. The water rates vary, depending on whether the bill is for home use, commercial use, or industrial use. A code of r means residential use, a code of c means commercial use, and a code of i means industrial use. Any other code should be treated as an error. The water rates are computed as follows:Three types of customers and their billing rates: Code...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT