Review Exercise 4.5 Solutions

    1. In both cases if you used or instead of and then there would be values that would get you out of the loop.
    2. no value of response will stop the loop (it can't be two different characters at the same time)
    3. no value of response will stop the loop (it can't be less than 'A' and at the same time greater than 'E')
    1. C
       D
        E
         F
          G
           H
    2. 50 51 52 53 54 
      40 41 42 43 44 
      30 31 32 33 34 
      20 21 22 23 24 
      10 11 12 13 14
    3. 40 33 26 19 12 5
      1. var count : int := 2
        loop
           if count mod 10 = 0 then
              put ""
           end if
           put count, " ", count ** 2
           count := count + 2
           exit when count > 40
        end loop 
      2. var power : int
        for i : 1 .. 5
           for j : 0 .. 4
              power := 1
              for k : 1 .. j
                 power := power * i
              end for
              put "    ", i, "\^", j, "  ", power
          end for
          put ""
        end for 
      1. 2 4
        4 16
        6 36
        8 64
        
        10 100
        12 144
        14 196
        16 256
        18 324
        
        20 400
        22 484
        24 576
        26 676
        28 784
        
        30 900
        32 1024
        34 1156
        36 1296
        38 1444
        
        40 1600
      2.     1^0  1
            1^1  1
            1^2  1
            1^3  1
            1^4  1
        
            2^0  1
            2^1  2
            2^2  4
            2^3  8
            2^4  16
        
            3^0  1
            3^1  3
            3^2  9
            3^3  27
            3^4  81
        
            4^0  1
            4^1  4
            4^2  16
            4^3  64
            4^4  256
        
            5^0  1
            5^1  5
            5^2  25
            5^3  125
            5^4  625
    1. 540 has 3 3's
    2. To find the number of times divisor (3) goes into number.
  1. const width : int := 5
    var divisor, quotient, remainder : int
    
    divisor := 1
    for position : 1 .. width -1
       divisor := divisor * 10
    end for
    
    get remainder
    % added this line
    put "$"..
    for decreasing position : width .. 1
       quotient := remainder div divisor
       % added the if statement - originally only the statement after the else was there
       if remainder < divisor then
          put "*"..
       else 
          put quotient ..
       end if
       remainder := remainder mod divisor
       divisor := divisor div 10
    end for
    put "" 
  2. var number : int
    
    put "Enter a natural number"
    get number
    
    put number, " has the following exact divisors: 1"..
    
    if number not= 1 then
       put ", "..
       for count : 2 .. number div 2
          if number mod count = 0 then
             put count, ", "..
          end if
       end for
       put number..
    end if
    
    put "." 
  3. var numDays, firstDay : int
    var current : int := 1
    
    put "How many days in the month?"
    get numDays
    put "What day is the first day? (1 = Sunday, 2 = Monday, 3 = Tuesday,"
    put "4 = Wednesday, 5 = Thursday, 6 = Friday, 7 = Saturday"
    get firstDay
    
    put "Sun  Mon  Tue  Wed  Thu  Fri  Sat"
    % handle first week
    for i : 1 .. 7
       if i < firstDay then
          put "     "..
       else
          put "  ", current, "  "..
          current := current + 1
       end if
    end for
    
    % print the rest of the weeks
    loop
       put ""
       exit when current > numDays
       for i : 1 .. 7
          % Don't go past the end of the month
          if current <= numDays then
             if current < 10 then
                put "  ", current, "  "..
             else
                put " ", current, "  "..
             end if
             current := current + 1
          end if
       end for
    end loop 
  4. % n - the current number to check if it is prime
    % factors - counts the number of factors of n (if > 2 then not prime)
    % count - the number of primes (we are trying to find the first 5000
    var factors, count : int := 0
    var n : int := 1
    
    loop
       factors := 0
       n := n + 1
    
       % find the number of factors for the current n
       for i : 1 .. n
          if n mod i = 0 then
             factors := factors + 1
          end if
       end for
    
       % if it is prime then print it
       if factors <= 2 then
          put n, " "..
          count := count + 1
          % print them 10 to a line
          if count mod 10 = 0 then
             put ""
          end if
       end if
       exit when count = 500
    end loop