## A22.0002.001 Midterm 2

1. (15 points) Write a program that produces all three-letter combinations in which the second letter is a vowel. Use all uppercase letters. In order to do this, write a procedure in which the parameter is the second letter of the combination.

```    PROGRAM ThreeWord;
{ Produce all 3-letter words that have a vowel as their 2 letter }

PROCEDURE Combination(middle : char);

VAR first, last : char;

BEGIN

FOR first := 'A' TO 'Z' DO
BEGIN
FOR last := 'A' TO 'Z' DO
BEGIN
writeln(first, middle, last);
END;
END;

END; { Combination }

VAR middle : char;

BEGIN

FOR middle := 'A' TO 'U' DO
BEGIN
IF middle IN ['A','E','I','O','U'] THEN
Combination(middle);
END;

END.
```

2. (15 points) Write a procedure that produces the following pattern:
```   #     #
#   #
# #
#
```
The height of the pattern and the symbol used are passed as arguments to the procedure.

```    PROCEDURE VPattern(height : integer; symbol : char);
{ Produce a V pattern of given height using given symbol }

VAR index, spaces : integer;

BEGIN

{ Compute the initial number of spaces between symbols plus width of }
{ symbol: 2 * (height - 1) }

spaces := 2 * (height - 1);

FOR index := 1 TO height DO
BEGIN

{ The first symbol is written in a field of the same width as the }
{ line number. }

write(symbol:index);

{ The second symbol is written for all but the last line }

IF index < height THEN
write(symbol:spaces);

{ Go to next line }

writeln;

{ Spaces decrement by 2 }

spaces := spaces - 2;
END;

END;
```

3. (15 points) What does the following program produce ?
```PROGRAM Q2;
VAR first, second : integer;

PROCEDURE PROC1(VAR first, second : integer);
BEGIN
write(first, ' + ', second, ' = ');
first := first + second;
writeln(first);
END;

PROCEDURE PROC2(first, second : integer);
BEGIN
write(first, ' * ', second, ' = ');
first := first * second;
writeln(first);
END;

BEGIN
first := 1;
second := 1;

WHILE (first < 5) AND (second < 4) DO
BEGIN
IF first > second THEN
PROC1(first, second);
ELSE
BEGIN
PROC2(first, second);
first := first + 1;
END;
END;
END.
```
```   1 * 1 = 1
2 + 1 = 3
3 + 1 = 4
4 + 1 = 5
```

4. (15 points) We are writing a program that allows to search a library's catalog by either Author, or Title or Subject. Assume that the procedures to perform the three operations have been already been written. You have to write the Prompt procedure that accepts a one-character input. The procedure will continue to loop until the character 'E' (exit) has been entered. In case the character is either 'A', or 'S', or 'T', Prompt will call the procedure ByAuthor, BySubject and ByTitle, respectively (i.e., if the user enters 'A', then it should call ByAuthor, etc.).

The procedure should be able to handle both upper and lower case letters and has to be able to deal with erroneous entries (e.g., handle the case in which the user has entered 'z' by mistake).

```   PROCEDURE Prompt;

{ Prompt for action then call appropriate procedure }

VAR action : char;

CONST prompt = '(A)uthor, (S)ubject, (T)itle, (E)xit > ';

BEGIN

write(prompt);

WHILE (action <> 'E') AND (action <> 'e') DO
BEGIN

CASE action OF

'A', 'a' : ByAuthor;
'S', 's' : BySubject;
'T', 't' : ByTitle;
ELSE
writeln('*** Incorrect Input: ', action);
END;

write(prompt);
END;
END;
```

5. (15 points) What will the following program produce ?
```   PROGRAM Q6;
VAR index : integer;

BEGIN

index := 1;

WHILE index < 7 DO
BEGIN

IF index < 4 THEN
IF index < 2 THEN
writeln('First breakpoint')
ELSE
writeln('Second breakpoint');

IF NOT (index in [1,3,5]) THEN
writeln('Third breakpoint');

index := index + 1;
END;
END.
```
```    First breakpoint
Second breakpoint
Third breakpoint
Second breakpoint
Third breakpoint
Third breakpoint
```

6. (25 points) Evaluate the following:
a) ('a' < 'b') AND (5 >= 4) OR NOT('A' in ['B'..'H'])
b) ('a' > 'b') AND (5 <= 4) OR NOT('A' in ['B'..'H'])
c) (9 < 1 OR 3 > 1) AND (5 < 3 OR 'j' > 'b')
d) (i < 5) AND (i > 10)
e) (i > 20) OR (i < 10) OR (i > 9 AND i < 21)

```   a) True