Review Exercise 3.5 Solutions

    1. All values of x will make it true (always true).
    2. No values of x will make it true (always false).
  1. a, d, e, f
    1. true and true = true
    2. not(true and false) = not false = true
    3. true or false and false = true or false = true
    4. not false and false = true and false = false
    1. not true = false
    2. false
    3. illegal, can't subtract chars
    4. not 2 = 1 ---> not false = true
    5. false or true = true
    6. 5 < 2 * 5 - 3 < 10
      5 < 7 < 10
      true < 10 which is illegal, incompatible types
  2. if flag then
       x := 0
    else
       x := 1
    end if 
    1. VC       The client preferred vinegar, crinkled chips
    2. BR       The client preferred bar-b-que, regular chips
    3. VT       The client preferred vinegar chips
    4. BC       The client preferred bar-b-que, crinkled chips
    5. CV       The client preferred other chips
    6. V        The client preferred vinegar chips
  3. if p and q then
       put "Both true"
    elsif p and not q then
       put "Only first true"
    elsif not p and q then
       put "Only second true"
    else
       put "Neither true"
    end if 
    1. Prints an appropriate message if the 3 variables are either in ascending or descending order.
    2. The brackets are not necessary since ands get done before ors any way.
    3. if a < b then
         if b <= c then
            put "Values are in order"
         else
            put "Values are out of order"
         end if
      elsif a > b then
         if b >= c then
            put "Values are in order"
         else
            put "Values are out of order"
         end if
      else
         put "Values are in order"
      end if 
    4. The one in part (c) would be more efficient because at most 3 comparisons would be needed. The original if always has 4 comparisons. However the original is much easier to understand than the one in part(c).
    1. % Assuming only lower case letters, otherwise you need to add the capitals
      case currChar of
         label 'a','e','i','o','u'  : charType := 'V'
         label 'b','c','d','f','g',
               'h','j','k','l','m',
               'n','p','q','r','s',
               't','v','w','x','y',
               'z'                  : charType := 'C'
         label '0','1','2','3','4',
               '5','6','7','8','9'  : charType := 'D'
         label '.',',',':',';','?',
               '!','-'              : charType := 'P'
         label '(',')','\'','"'     : charType := 'S'
         label                      : charType := 'O'
      end case 
    2. put "currChar was of type "..
      case charType of
         label 'V' : put "vowel"
         label 'C' : put "consonant"
         label 'D' : put "digit"
         label 'P' : put "punctuation mark"
         label 'S' : put "special character"
         label 'O' : put "other"
         label     : put "shouldn't be possible"
      end case 
  4. var amount, leftover, penny, nickel, dime, quarter : int
    var commas : int := -1
    
    put "How much money do we need to make change for (0-99 cents)?"
    get amount
    
    quarter := amount div 25
    leftover := amount mod 25
    dime := leftover div 10
    leftover := leftover mod 10
    nickel := leftover div 5
    penny := leftover mod 5
    
    if quarter > 0 then
       commas := commas + 1
    end if
    
    if dime > 0 then
       commas := commas + 1
    end if
    
    if nickel > 0 then
       commas := commas + 1
    end if
    
    if penny > 0 then 
       commas := commas + 1
    end if
    
    put amount, " cents requires "..
    
    case quarter of
       label 2, 3: put quarter, " quarters"..
       label 1   : put "1 quarter"..
       label     :
    end case
    
    if commas > 0 and quarter > 0 then
       put ", "..
       commas := commas - 1
    end if
    
    case dime of
       label 2: put "2 dimes"..
       label 1: put "1 dime"..
       label  :
    end case
    
    if commas > 0 and dime > 0 then
       put ", "..
       commas := commas - 1
    end if
    
    if nickel = 1 then
       put "1 nickel"..
    end if
    
    if commas > 0 then
       put ", "..
    end if
    
    case penny of
       label 1      : put "1 penny"..
       label 2, 3, 4: put penny, " pennies"..
       label        :
    end case
    
    put "."