4.5 Review Exercises


Exercise 4.5

  1. What value(s) of the variable response will stop the following loops?
    1. loop
         exit when response = 'y' and response = 'Y'
         .
         .
         .
      end loop 
    2. loop
         exit when response < 'A' and response > 'E'
         .
         .
         .
      end loop 
  2. What output will be produced by each program?
    1. var width : int := 0
      
      for letter : 'C' .. 'H'
         for i : 1 .. width
            put " "..
         end for
         put letter
         width := width + 1
      end for
    2. for decreasing i : 5 .. 1
         for j : 0 .. 4
            put i, j, " " ..
         end for
         put ""
      end for 
    3. var x : int := 40
      
      loop
         exit when x <= 0
         put x, " " ..
         x := x - 7
      end loop 
    1. Rewrite each program with the appropriate indentation and spacing to correctly show its structure.
      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 
    2. what output will be produced by each program?
    1. What will the following program print given input of 540?
      const divisor := 3
      var number, test, count : int
      
      get number
      test := number
      count := 0
      loop
         exit when test mod divisor not= 0
         test := test div 3
         count := count + 1
      end loop
      put number, " has ", count, " ", divisor, "'s" 
    2. Describe in a few words the purpose of the program.
  3. Frequently, when computers are used to write cheques, the amounts are printed with leading asterisks in order to prevent fraud. For example, the value 27 might be printed as $***27, rather than $   27. Modify the program shown in Exercise 4.2, Question 3 so that it has this effect.
  4. Write a program to read a natural number and then find and print all of its exact divisors.
  5. Write a program that takes as input the number of days in a month and the day of the week on which the first of the month occurs. The program should produce a display of the calendar for that month in standard form. For example, if there are thirty days in the month and the first day of the month is on a Wednesday, output should be similar to the following.

    Sun   Mon   Tue   Wed   Thu   Fri   Sat
    
                        1     2     3     4
                        
      5     6     7     8     9    10    11
      
     12    13    14    15    16    17    18
     
     19    20    21    22    23    24    25
     
     26    27    28    29    30

  6. A natural number, N, greater than one, is a prime number if it has no natural number divisors other than 1 and N. Write a program that finds the first five hundred primes, printing them ten to a line.