All your work is to be done on the exam. Be sure to write your name on each page of the exam.

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.

```_____  a. FOR J:= 0 TO 0 DO

_____  b. FOR J:= 1 DOWNTO 5 DO

_____  c. FOR CH:= '#' TO 7 DO

_____  d. FOR J:= '1' TO '5' DO

_____  e. FOR CH:= 5 DOWNTO 3 DO
```
```  a. 1

b. 0

c. invalid (initial value is a character, final value is an integer)

d. 5

e. 3
```

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

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

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

```   c. FOR J:= 4 TO 6 DO  BEGIN
FOR J:= 1 TO 5 DO BEGIN
write('*')
END;
writeln
END
```
Answer: invalid, inner loop uses same variable as outer loop.

3. (10 points) Indicate whether each of the following is valid or not. By valid we mean, whether it is executable code that can occur anywhere in a program. If it is not valid, explain why not. Note that a variable can be of any type

```(a) True AND True

(b) CASE Character OF
'a': writeln('one');
'2': writeln('Two');
'3': writeln('Three')
END{CASE};

(c) One OR Two AND Three

(d) 5 > '4'

(e) if x:=4 then writeln('ow')```
``` (a) valid

(b) valid

(c) valid

(d) invalid    (left side is an integer, right side is a character)

(e) invalid    (the boolean operator is "=" not ":=")

```

4. (5 points) Complete the program segment that adds the odd numbers from 1 to N, where N is read into the program. So if N is 8, then the program would add 1, 3, 5, 7.

```   writeln('enter your number');

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

Sum := _____

FOR J:= 1 TO ______ DO

IF _______________________then

Sum:=_________________
```
```   writeln('enter your number');

Sum := 0;

FOR J := 1 TO N DO

IF (J MOD 2 = 1) then

Sum := Sum + J;

```

5. (5 points) Complete the program segment that prints the value of the variable letter, if letter is a capital vowel. The vowels are (A, E, I, O, or U).

```   IF letter_____________________then

_________________
```
```   IF letter IN ['A', 'E', 'I', 'O', 'U'] then

writeln(letter);
```

6. (10 points) Complete the program segment that reads a group of numbers ending with a sentinel of -9 (a sentinel is the number or character that indicates the end of the data) and places the even numbers in an array called A. For instance, if the input were: 12 13 43 6 16 -9, the program would place 12 in A[1], 6 in A[2] and 16 in A[3].

```   VAR j, number:integer;
A: ARRAY[1..100] OF integer;
BEGIN

j:=________
WHILE number <> -9 DO
BEGIN
IF __________________________THEN
BEGIN
j:=________
A[____]:= Number;
END
________________
END
END.
```
```   VAR j, number:integer;
A: ARRAY[1..100] OF integer;
BEGIN

j:= 0;
WHILE number <> -9 DO
BEGIN
IF (number MOD 2 = 0) THEN
BEGIN
j:= j + 1;
A[j]:= Number;
END
END
END.
```

7. (10 points) What does the following program produce?

```   program pattern;
const star = '*';
VAR j:integer;
BEGIN
for j:= 4 downto 1 do
writeln(star:j, star:10 - (2 * j) );
for j:= 1 to 4 do
writeln(star:j, star:10 - (2 * j) );
END.
```
```   ___*_*
__*___*
_*_____*
*_______*
*_______*
_*_____*
__*___*
___*_*
```
Note: "_" indicates blank space.

8. (10 points) Given the following procedures:

```   procedure star(number:integer);
var j:integer;
begin
for j:= 1 to number Do
write('*');
writeln
end;

procedure plus(number:integer);
var j:integer;
begin
for j:= 1 to number Do
write('+');
writeln
end;
```
and your goal is to produce the following pattern:
```****
++++
***
+++
**
++
```
Complete the program that produces the pattern:
```   for j:=__________________DO
begin

________

________
end.
```
```   for j:= 4 DOWNTO 2 DO
begin

star(j);

plus(j);

end.
```

9. (10 points) Given the following heading: procedure pattern(number: integer; symbol:char); complete the procedure that when activated by, for instance, pattern(5,'*'), would produce

`*****`
and when activated by, for instance, pattern(3, '-') would produce
```   procedure pattern(number:integer; symbol:char);
VAR j:integer;
BEGIN
__________________
BEGIN

________________
END
END;
```
```   procedure pattern(number:integer; symbol:char);
VAR j:integer;
BEGIN
FOR j:= 1 TO number
BEGIN
write(symbol)
END
END;
```

10. (20 points) DO EITHER A OR B

A. Write a program that reads a sentence at least four characters long into an array of characters and prints every thing but the last three characters. The sentence must end with an asterisk. Thus if the sentence was

`   hello there*`
the program would print:
`   hello the`
as output. In other words, the re* would be stored in the array but would not be printed.

```   PROGRAM Reader;
VAR sentence : string[30];
index, count : integer;
letter : char;

BEGIN

{ Read first 3 characters }

FOR count := 1 TO 3 DO

{ Read fourth letter and proceed }

WHILE (letter <> '*' ) DO
BEGIN

count := count + 1;
sentence[count] := letter;

END;

FOR index := 1 TO count-3 DO
write(sentence[index]);

writeln;

END.
```

B. Write a program that reads a sentence that ends with a period, for instance,

`   hello there.`
and prints the sentence with each letter e replaced with an asterisk. Thus in our example the program would print
`   h*llo th*r*.`

```   PROGRAM SentenceReader;

VAR letter : char;

BEGIN

WHILE (letter <> '.' ) DO
BEGIN

CASE letter OF
'E', 'e': write('*');
ELSE
write(letter);
END;