_____ 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

a. 1 b.invalid('12' and '10' are strings, not ordinals) c. 3 d.invalid(values must be ordinals,6.0is 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

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 TONDO IF(J MOD 4) = 0then 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:**

IFletter in ['c'..'m']thenwriteln(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] ); WHILEB[j] <> '.'DO BEGIN {read data into array B} j :=j + 1; read( B[j] ) END; count :=j-1; j := 0; FOR k := 1 TOcountDO {puts character following a blank in array A} IFB[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

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 - - aaComplete the program that produces the pattern:

for j:=__________________DO begin _______________ _______________ end end

**Answer:**

for j:=4 DOWNTO 2DO begindiagonal(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

different := ______________ FOR j := ____TO_____ DO IF ________ <>__________THEN ___________ := ________________; IF__________________THEN writeln('The arrays are__________________') ELSE writeln('The arrays are__________________') END.

**Answer:**

different :=False;FOR j :=1TONDO IFX[j]<>Y[N-j+1]THENdifferent:=True; IFdifferentTHEN writeln('The arrays arenot in reverse order.') ELSE writeln('The arrays arein reverse order.') END.

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

var X : array [1..max] of integer;where

- Reads in a positive integer, say
`k`. - Stores it in
`X[k]`if`X[k]`is unoccupied, or - If
`X[k]`is occupied, stores it in the next available unoccupied location of`X`. So if 3, 4, 5 have already been read and thus`X[3] = 3, X[4] = 4, X[5] = 5, X[6] = 0`and 3 is now read as input, place that 3 in`X[6]`. Use a`while`loop to do this.

**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;