2/1/2000 Thomas Yan ------------------------------------------------------------------------------- suppose int variables a, b, c have been assigned values. here are various attempts to print the maximum value, with some questions for you to think about. we recommend that you hand-trace and then actually run each program segment on the following examples: a b c 2 3 9 3 9 9 9 9 9 we also recommend that you consider how hard it would be to extend each valid approach to dealing with 5 variables (a, b, c, d, e). "exponential tree" approach -- do you see how this approach grows cumbersome very quickly as the number of variables increases? if (a >= b) if (a >= c) System.out.println("the max is " + a); else System.out.println("the max is " + c); else if (b >= c) System.out.println("the max is " + b); else System.out.println("the max is " + c); "exponential tree" with auxiliary variable -- do you see how the print style is centralized and hence easy to modify? int max; if (a >= b) if (a >= c) max = a; else max = c; else if (b >= c) max = b; else max = c; System.out.println("the max is " + max); "quadratic" attempt -- do you see why this "needs" $>=$ and $else$? if (a > b && a > c) System.out.println("the max is " + a); if (b > a && b > c) System.out.println("the max is " + b); if (c > a && c > b) System.out.println("the max is " + c); "quadratic" approach -- why does $b>=c$ instead of $b>=a && b>=c$ suffice? if (a >= b && a >= c) System.out.println("the max is " + a); else if (b >= c) System.out.println("the max is " + b); else System.out.println("the max is " + c); "linear, loop-like" approach -- do you agree this is the easiest approach to extend to 5 variables? int max; // max so far max = a; if (b > max) max = b; if (c > max) max = c; System.out.println("the max is " + max); "linear" attempt -- why doesn't this work? int max; max = a; if (b > max) max = b; else if (c > max) max = c; System.out.println("the max is " + max);