Exercise 26 - CBD Page 120

Solution index                   Textual C program


#include <stdio.h>

int main(void){

  int x, y, z, w;
  int res1, res2, res3;

  printf("Enter the integers x, y, z, and w.\n");
  scanf("%d%d%d%d", &x, &y, &z, &w);

  /* min(x,y) */
  res1 = (x < y) ? x : y;


  /* min(x,y,z) =
     if x < y      
     then min(x,z)      y ruled out
     else min(y,z)      x ruled out
     Now use the pattern from above.
  */
  res2 = 
   (x < y)?
    ((x < z) ? x : z):
    ((y < z) ? y : z);


  /* max(x,y,z,w) =
     if x < y
     then max(y,z,w)    x ruled out
     else max(x,z,w)    y ruled out

     max(x,y,z) =
     if x < y      
     then max(y,z)      x ruled out
     else max(x,z)      y ruled out

     max(x,y) =
     if x < y then y else x
     
  */
  res3 =
   (x < y)?
     ((y < z) ? 
      ((z < w) ? w : z) :
      ((y < w) ? w : y)) :
     ((x < z) ? 
      ((z < w) ? w : z) :
      ((x < w) ? w : x));

  printf("The minimum of %d and %d is %d.\n", x, y, res1);
  printf("The minimum of %d, %d, and %d is %d.\n", x, y, z, res2);
  printf("The maximum of %d, %d, %d, and %d is %d.\n", x, y, z, w, res3);

}

This is a solution to exercise 26 on page 120 of C by Dissection. Notice that we use mathematical notation in the comments of the program, not C notation.
 

  int x, y, z, w;
  int res1, res2, res3;

  printf("Enter the integers x, y, z, and w.\n");
  scanf("%d%d%d%d", &x, &y, &z, &w);
Variables and declared and input is read via scanf.
 

  res1 = (x < y) ? x : y;
The minimum of x and y is found by means of a single conditional expression. It says that if x < y then x is returned, else y. This is indeed the minimum of x and y.
 

  res2 = 
   (x < y)?
    ((x < z) ? x : z):
    ((y < z) ? y : z);
Here we go for the minimum of three variables: x, y and z. The crucial observation is that min(x,y,z) is min(x,z) if x is less than y. If x >= y, min(x,y,z) is min(y,z). With this insight, we can use the idea from above to find min(x,y,z) using nested conditionals.
 

In the rest of this dissection we handle max(x,y,z,w). Let me warn you that this is rather difficult. The approach that we use here is solely an exercise in using the conditinonal expressions inside each other. In a practical situation we would never program max(x,y,z,w) in the way we do below...
 

  res3 =
   (x < y)?
     ((y < z) ? 
      ((z < w) ? w : z) :
      ((y < w) ? w : y)) :
     ((x < z) ? 
      ((z < w) ? w : z) :
      ((x < w) ? w : x));
We now find max(x,y,z,w). Similar to the situation above, max(x,y,z,w) is max(y,z,w) if x < y. If not, max(x,y,z,w) is max(x,z,w). We can now use nested conditionals again, and a pattern for max(a,b,c). Notice that it may be quite difficult get the details right, unless you do very systematic name substitutions in the program pieces just developed.
 

  printf("The minimum of %d and %d is %d.\n", x, y, res1);
  printf("The minimum of %d, %d, and %d is %d.\n", x, y, z, res2);
  printf("The maximum of %d, %d, %d, and %d is %d.\n", x, y, z, w, res3);
We print the results, as assigned to the variables res1, res2, and res3.
 


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