Exercise 24 - CBD Page 119

Solution index                   Textual C program


/* Print a table of values for some boolean functions. */

#include <stdio.h>

char bool (int i);

int main(void)
{
   int   b1, b2, b3, b4;       /* boolean variables */
   int   cnt = 0;

   /*headings*/
   printf("\n%5s%5s%5s%5s%5s%7s%7s\n\n",
      "Cnt", "b1", "b2", "b3", "b4",
      "fct1", "fct2");

   for (b1 = 0; b1 <= 1; ++b1)
      for (b2 = 0; b2 <= 1; ++b2)
         for (b3 = 0; b3 <= 1; ++b3)
            for (b4 = 0; b4 <= 1; ++b4)
              printf("%5d%5c%5c%5c%5c%7c%7c\n",
                     ++cnt, bool(b1), bool(b2), bool(b3), bool(b4),
                     bool(b1 || b2 || b3 || b4),
                     bool(!(!b1 || b2) && (!b3 || b4))
                    );
   printf("\n");
   return 0;
}

char bool (int i){
  if (i==0) return 'F'; else return 'T';
}

This is a solution to exercise 24 on page 119 of C by Dissection.
 

char bool (int i);
We take the program from page 97 as the starting point. Only few modifications are needed. This fragment is a 'forward declaration' - a so-called function prototype. It tells the compiler that bool is a function that takes an integer as input, and gives a character as output. The function will be declared in full later in the program.
 

   int   b1, b2, b3, b4;       /* boolean variables */
   int   cnt = 0;
The necessary variables are declared
 

   printf("\n%5s%5s%5s%5s%5s%7s%7s\n\n",
      "Cnt", "b1", "b2", "b3", "b4",
      "fct1", "fct2");
The following header line is printed:
  Cnt   b1   b2   b3   b4   fct1   fct2
 

   for (b1 = 0; b1 <= 1; ++b1)
      for (b2 = 0; b2 <= 1; ++b2)
         for (b3 = 0; b3 <= 1; ++b3)
            for (b4 = 0; b4 <= 1; ++b4)
              printf("%5d%5c%5c%5c%5c%7c%7c\n",
                     ++cnt, bool(b1), bool(b2), bool(b3), bool(b4),
                     bool(b1 || b2 || b3 || b4),
                     bool(!(!b1 || b2) && (!b3 || b4))
                    );
This is the key part of the program. In four nested for loops, which iterates each of the 'boolean' b variables from 0 to 1, provide for all possible combination of the four boolean values. In total, 2*2*2*2 = 16 cases are encountered. In the inner for loop, a printf command is located. It prints the boolean values of b1, ..., b4, and the boolean values of the expressions (b1 || b2 || b3 || b4) and (!(!b1 || b2) && (!b3 || b4)). The output is:
    1    F    F    F    F      F      F
    2    F    F    F    T      T      F
    3    F    F    T    F      T      F
    4    F    F    T    T      T      F
    5    F    T    F    F      T      F
    6    F    T    F    T      T      F
    7    F    T    T    F      T      F
    8    F    T    T    T      T      F
    9    T    F    F    F      T      T
   10    T    F    F    T      T      T
   11    T    F    T    F      T      F
   12    T    F    T    T      T      T
   13    T    T    F    F      T      F
   14    T    T    F    T      T      F
   15    T    T    T    F      T      F
   16    T    T    T    T      T      F
 

char bool (int i){
  if (i==0) return 'F'; else return 'T';
}
A full declaration of the the function bool is found here. If the input is zero the character 'F' is returned. Else, the character 'T' is returned. Notice that we have not taken the hint in the exercise formulation into account.
 


Generated: Wednesday, March 29, 2006, 12:33:20
This program dissection page is generated from an XML-in-LAML source file