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 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
(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...
Use if statements to write a Java program that inputs a single letter and prints out...
Use if statements to write a Java program that inputs a single letter and prints out the corresponding digit on the telephone. The letters and digits on a telephone are grouped this way: 2 = ABC    3 = DEF   4 = GHI    5 = JKL 6 = MNO   7 = PRS   8 = TUV 9 = WXY No digit corresponds to either Q or Z. For these 2 letters your program should print a message indicating that they are not...
Write a C program that creates and prints out a linked list of strings. • Define...
Write a C program that creates and prints out a linked list of strings. • Define your link structure so that every node can store a string of up to 255 characters. • Implement the function insert_dictionary_order that receives a word (of type char*) and inserts is into the right position. • Implement the print_list function that prints the list. • In the main function, prompt the user to enter strings (strings are separated by white-spaces, such as space character,...
Write a program calls each of the three methods described below and prints out the result...
Write a program calls each of the three methods described below and prints out the result of the two methods which return value reverse- a void method that reverses the elements of the array. Print the array before you call the method and after call the method. getMin- an int method that returns the smallest value in the array Sample output array: 22,34,21,35,12,4,2,3,99,81 array in reverse: 81,99,3,2,4,12,35,21,34,22 the smallest number is 2
Write a program in C++ that prints out the even numbers between 1 and 21 using...
Write a program in C++ that prints out the even numbers between 1 and 21 using WHILE loop. Also, find the sum AND product of these numbers and display the resulting sum and product.
Write a c++ program that prints the count of all prime numbers between A and B...
Write a c++ program that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = Any 5 digit unique number B = A + 1000 Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2, 5, 7, 11, 13, 17, 19, 23, and 29. Rules:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT