1. (10 points) How many times would the FOR-DO loops beginning with the following statements be executed? If the loop is invalid, write invalid and give the reason. Note that the variables can be of any type
_____   a. FOR J:=  a  TO a DO

_____   b. FOR J:=  '12' DOWNTO '10' DO

_____   c. FOR CH:= -1 TO 1 DO

_____   d. FOR J:=  6.0 TO 5 DO

_____   e. FOR CH:= 1 DOWNTO -1 DO
Answer:

   a. 1

   b. invalid ('12' and '10' are strings, not ordinals)

   c. 3

   d. invalid (values must be ordinals, 6.0 is a real)

   e. 3


2. (10 points) How many times would the write in each of the following loops be executed? If the loop is invalid, write invalid. and give the reason.

        a. FOR J:= 3 DOWNTO 3 DO BEGIN
              FOR K:= 3 TO 3 DO BEGIN
                  write('*')
              END; 
              writeln
           END

        b. FOR J:= 1 TO 4 DO  BEGIN
              FOR K:= 2 TO J DO BEGIN
                  write('*')
              END;
              writeln
           END

        c. FOR j:= 4 TO 6 DO  BEGIN
              FOR j:= 5 TO 1 DO BEGIN
                  write('*')
              END;
              writeln
           END
Answer:

   a. 1

   b. 6

   c. invalid, inner loop uses same variable as outer loop.


3. (20 points) You are given an array X defined by var X : array[0..max] of integer where max is defined in a const definition, and input consisting of four-digit integers, for instance, 1023, 1141, 3636, and ending with the sentinel -9. Write a program that first places zeros in all the elements of the array, i.e., X[0] := 0, X[1] := 0. etc. Then the program should place the data into that element with the subscript given by the last two digits of the four-digit integer input. Thus X[23] would store 1023, X[41] would store 1141, etc. Assume that the last two digits of the integer are unique, that is, there is only one integer that ends with 41, in our case, 1141.

Answer:

   PROGRAM Store;
   VAR inp, ind : integer;
       X : array [0..max] of integer;   {max defined elsewhere }

   BEGIN

      { Initialize }

      FOR ind := 0 TO max DO
         X[ind] := 0;

      { Read values and store into array, 'till you hit -9 }

      read(inp);
      WHILE (inp <> -9) DO
         X[inp MOD 100] := inp;

   END.


4. (5 points) Complete the program segment that adds the numbers that are divisible by four for the numbers from 1 to N, where N is read into the program. So if N is inputted as 12, then the program would add 4, 8 and 12.

   writeln('enter your number');

   ----------------

   Sum := _____

   FOR J:= 1 TO ______ DO

       IF _______________________then

           Sum:=_________________

Answer:

   writeln('enter your number');

   readln(N);

   Sum := 0;

   FOR J:= 1 TO N DO

       IF (J MOD 4) = 0 then

           Sum:= Sum + J;


5. (5 points) Complete the program segment that prints the value of the variable letter, if letter comes between 'c' and 'm' in the alphabet.

   IF __________________________then

            _________________

Answer:

   IF letter in ['c'..'m'] then

            writeln(letter);


6. (10 points) Complete the program segment that reads a group of characters ending with a '.' into an array B, and places the first character that follows each blank in the sentence in successive elements of an array called A. For instance, if the input were: 1a 8 4b5 d7(., the program would place '8' in A[1], '4' in A[2] and 'd' in A[3].

   VAR j, k:integer;
       A, B: ARRAY[1..100] OF char;
   BEGIN
        writeln('type your input');
        j:=________;
        read( B[ ___ ]  );
        WHILE ________________ DO BEGIN {read data into array B}
           j := _____;
           read( B[ ___ ]  )
        END;
        count :=  ______;
        j := 0;
        FOR k := 1 TO  ____ DO 
        {puts character following a blank in array A}
          IF __________________________THEN
          BEGIN
             j:= j + 1;
             A[____]:= B[____ ];
          END {IF};
   END.

Answer:

   VAR j, k:integer;
       A, B: ARRAY[1..100] OF char;
   BEGIN
        writeln('type your input');
        j:=1;
        read( B[ j ]  );
        WHILE B[j] <> '.' DO BEGIN {read data into array B}
           j := j + 1;
           read( B[ j ]  )
        END;
        count :=  j-1;
        j := 0;
        FOR k := 1 TO  count DO 
        {puts character following a blank in array A}
          IF B[k] = ' 'THEN
          BEGIN
             j:= j + 1;
             A[j]:= B[k+1 ];
          END {IF};
   END.


7. (10 points) Show the pattern the following program produces?

   program pattern;
   const one = 'a';
   VAR j:integer;
   BEGIN
        for j:= 1  to 4 do
            writeln(one:j);
        for j:= 1 to 4 do
            writeln( chr( ord(one) + j ):j)
   END.

Answer:

   a
   _a
   __a
   ___a
   b
   _c
   __d
   ___e
Note: _ indicates blank space.


8. (5 points) Given the following procedures:

   procedure diagonal(number:integer; ch : char);
   var j:integer;
   begin
      for j:= 1 to number Do
          writeln(ch : j)
   end;

   procedure line(number:integer; ch : char);
   var j:integer;
   begin
      for j:= 1 to number Do
          write(ch);
      writeln
   end;
and your goal is to produce the following pattern:
-
 -
  -
   -
aaaa
-
 -
  -
aaa
-
 -
aa
Complete the program that produces the pattern:
         for j:=__________________DO
          begin

                _______________

                _______________
             end
          end

Answer:

         for j:= 4 DOWNTO 2 DO
          begin

                diagonal(j, '-');

                line(j, 'a');
             end
          end


9. (10 points) Given the following declaration:

   VAR x, y : array[1..n] of char;
       j : integer;
       different : boolean;
where n is given in a const definition and the elements of the arrays x and y have already been given values. Complete the following program segment that determines whether the values of the elements of the two arrays are in reverse order. Thus if x[1] is 'a', x[2] is 'b', and x[3] is 'c'; and y[1] is 'c', y[2] is 'b' and y[3] is 'a', then the elements of the two arrays are in the reverse order:
       different := ______________   

       FOR j := ____TO_____ DO
  
          IF  ________ <>__________THEN
    
             ___________ := ________________;

       IF__________________THEN
       
           writeln('The arrays are__________________')
       ELSE
           writeln('The arrays are__________________')
   END.

Answer:

       different := False;   

       FOR j := 1 TO N DO
  
          IF  X[j] <> Y[N-j+1] THEN
    
             different := True;

       IF different THEN
       
           writeln('The arrays are not in reverse order.')
       ELSE
           writeln('The arrays are in reverse order.')
   END.


10. (15 points) Assume that you have declared an array:

   var X : array [1..max] of integer;
where max is very large, and that all the elements of the array have been initialized to 0, indicating that all the locations are unoccupied. Write a program segment that:

Answer:

   { read number and store value }

   read(k);
   value := k;

   { Loop through array looking for empty slot }

   WHILE (X[k] <> 0) AND (k < max) DO
      k := k + 1;

   { Store value (making sure we don't blow apart array) }

   IF (k <= max) THEN
      X[k] := value;