3.5 Review Exercises


Exercise 3.5

  1. For what value(s) of x will each expression have the value true?
    1. (x not= 2) or (x not= 3)
    2. (x = 2) and (x = 3)
  2. State which of the following comparisons are true.
    1. "James" <= "Jim"
    2. "Tom" > "tomorrow"
    3. "025" >= "25"
    1. "=" <= "=="
    2. "!*&^~" < "$@?!%"
    3. "this" < "this "
  3. Assume that you have a program that has the following declarations:
    var age : int := 16
    var height : int := 175
    var weight : int := 70
    var sex : char := 'M'
    var healthy : boolean := true 
    Using these values, evaluate each expression.
    1. (age >= 16) and healthy
    2. not (weight <= 75 and height >= 180)
    3. age > 10 or sex = 'F' and height < 170
    4. not height < 160 and weight < 60
  4. For each legal expression, state its value. For each illegal expression, state the reason that it is illegal.
    1. not(3 < 4.5)
    2. 'r' < 'q'
    3. '6' -'2' = '4'
    1. not 17 mod 5 = 1
    2. 'G' = 'g' or 1 + 2 = 3
    3. 5 < 2 * 5 - 3 < 10
  5. Rewrite the following fragment in as concise a form as possible. Assume that flag is boolean and x is int.
    if true then
       if not flag then
          x := 1
       else
          x := 0
       end if
    end if 
  6. Study the following program and then answer the questions that follow it.
    var flavour, style : char
    
    put "Enter flavour and style"
    get flavour, style
    
    
    put flavour, style ..
    put "       The client preferred " .. 
    
    if flavour = 'B' then
        put "bar-b-que" ..
    elsif flavour = 'V' then
        put "vinegar" ..
    else
        put "other" ..
    end if
    
    if style = 'C' then
        put ", crinkled" ..
    elsif style = 'R' then
        put ", regular" ..
    end if
    
    put " chips" 
    What would the program print given each set of data as input?
    1. VC
    2. BR
    3. VT
    1. BC
    2. CV
    3. V R
  7. Rewrite using nesting.
    if p and q then
       put "Both true"
    end if
    if p and not q then
       put "Only first true"
    end if
    if not p and q then
       put "Only second true"
    end if
    if not p and not q then
       put "Neither true"
    end if 
  8. Consider the following statement.
    if (a <= b and b <= c) or (a >= b and b>= c) then
       put "Values are in order"
    else
       put "Values are out of order"
    end if 
    1. Describe in a few words the effect of the statement.
    2. Are the brackets necessary? Justify your answer.
    3. Write a statement that has the same effect but uses neither and nor or.
    4. Which statement is more efficient? Justify your answer.
  9. As part of a program to analyze text, a fragment is required to record the type of character that is now in the variable currChar.
    1. Write a fragment that sets the char variable charType to the appropriate value given by the following table.
    2. currChar
      charType
      vowel
      'V'
      consonant
      'C'
      digit
      'D'
      punctuation mark .,:;?!-
      'P'
      special character ()'"
      'S'
      other character
      'O'
    3. Assuming that the instructions of part a) have been executed, write a case statement that will print a message indicating the category into which currChar falls: vowel, consonant, digit, punctuation mark, special character, or other.
  10. Write a program that makes change for amounts less than one dollar. Input to the program should be a positive integer less than 100, representing an amount of money, in cents. Output should be the original amount of money together with a set of coins (quarters, dimes, nickels, pennies) that could make up that amount. The program should produce change containing the minimum number of coins required for the given amount. The program should print results in a reasonable, non-stilted way. For example, input of 58 should produce results something like:

    58 cents requires 2 quarters, 1 nickel, 3 pennies.

    rather than

    58 cents requires 2 quarters, 0 dimes, 1 nickels, 3 pennies.