In: Computer Science
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++;
};
};
};
};
};
};
};
};
}
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++;
};
};
};
};
};
};
};
};
}