6.4 Solutions

    1. mystery
    2. a and b
    3. int
    4. function mystery (a, b : real) : int
    5. If the first parameter is smaller than the second it returns -1, if it is bigger returns 1 and if they are equal it returns 0
    1. % returns the argument that is smaller (comes first in ASCII)
      function first (a, b : char) : char
         if a < b then
            result a
         else
            result b
         end if
      end first 
    2. % returns true if the argument is an even number
      % false if it is an odd number
      function even (value : int) : boolean
         var temp : boolean := false
         if value mod 2 = 0 then
            temp := true
         end if
         result temp
      end even 
    1. first and second
    2. height and width
    3. int
    4.  
      The height is 12 and the width is 8
      They differ by: 4
    5. It wouldn't make any difference. PosDiff finds the positive difference so it automatically subtracts the smaller argument from the bigger one.
    6. function posDiff (first, second : int) : int
         result abs(first - second)
      end posDiff 
  1. (c) is invalid because put is not a function. There is no result to store in x.
    (d) is invalid because abs is a function. You need to do something with the result. You have to say put abs(x) or x := abs(x). You can't call it like a procedure.
  2. % a
    function ceiling (number : real) : int
       if number >= 0 then
          if round(number) = number then
            result round(number)
          else
            result round(number + 0.5)
          end if
       else
          if round(number) = number then
            result round(number)
          else
            result round(number - 0.5)
          end if
       end if
    end ceiling
    
    % b
    put ceiling(3.1)
    put ceiling(25.9)
    put ceiling(67.0)
    put ceiling(0.0)
    put ceiling(-1.1)
    put ceiling(-67.5)
    put ceiling(-15.9)
    
        
     
  3. function meanValue(a : array 1 .. * of int) : real
       var sum: int := 0
       
       for i : 1 .. upper(a)
          sum := sum + a(i)
       end for
    
       result sum / upper(a)
    end meanValue 
    
    
    
  4. function smallest(a : array 1 .. * of real) : real
       var small: real := a(1)
       
       for i : 1 .. upper(a)
          if a(i) < small then
             small := a(i)
          end if
       end for
    
       result small
    end smallest
    
     
  5. % a
    function numDigits(number : int) : int
        var count : int := 1
        var temp : int := number
        
        loop
           temp := temp div 10
           exit when temp = 0
           count := count + 1
        end loop
        
        result count
    end numDigits
    
    % b
    var a : array 1 .. 6 of int
    for i : 1 .. 6
        put "Enter number ", i, " of 6"
        get a(i)
    end for
    
    put "Number    Number of Digits"
    put "--------------------------"
    for i : 1 .. 6
        put a(i):6, numDigits(a(i)):12
    end for