## A22.0002.001 Midterm

1. (30 points) Write a program that reads two inputs from the keyboard: a number indicating how many letters will be typed, and a sequence of letters. The program should then produce an output in which each letter is printed on a new line and the letter's horizontal position on the line should correspond to letter's position in the alphabet; i.e.: 'a' in position 1, 'b' in position 2, etc. You can assume that all the letters will be typed in lower case.

Sample screen:

```    Number of letters: 7
Enter letter sequence: abcdefg
```
 a b c d e f g
Note: the table lines are there only to make it easier for you to read the output, you are not supposed to draw the table.

Hints, hints, hints:

• Since the program repeats a set of statements, you should be using a FOR-Loop.
• Since the letters are printed at different positions, you should be using a formatted output.
• Letters in the alphabet are ordered according to their ASCII value.
```PROGRAM Letters;

VAR numLetters, index, position : integer;
inpChar : char;

BEGIN

write('Number of letters: ');
write('Enter letter sequence: ');

FOR index := 1 TO numLetters DO
BEGIN

{ The position of the letter will be it's position in
the alphabet. Therefore: a -> 1, b -> 2, etc.       }

position := ord(inpChar) - ord('a') + 1;
writeln(inpChar:position);
END
END.
```

2. (20 points) What does the following program produce ?
```   PROGRAM Q2;

VAR lhs, rhs : integer;

BEGIN

FOR lhs := 1 TO 4 DO
BEGIN
FOR rhs := 1 TO 4 DO
write(lhs * rhs:2);
writeln;
END
END.
```
```    1 2 3 4
2 4 6 8
3 6 912
4 81216
```

3. (10 points) If in a program you have the following assignments:
```   A := '1';
B := 12;
C := 'a';
A := 'valerio';
D := 3;
B := 5.5;
E  := True;
```
Which would be the correct declarations ?
```   VAR A : string[7]_ ;
B : real______ ;
C : char______ ;
D : integer___ ;
E : boolean___ ;
```

Hints, hints, hints:

• Careful, some variables are used in more than one assignment.

4. (20 points) How many times will the following FOR-Loops execute ?
```1. FOR i := -1 TO 5 DO            7___
2. FOR i := 8 TO 6 DO             0___
3. FOR i := 'e' DOWNTO 'c' DO     3___
4. FOR i := 2 DOWNTO -2 DO        5___
5. FOR i := 1 DOWNTO 5 DO         0___
```

5. (20 points) Use the following declarations for all the questions below.
```   VAR str : string[20];
firstInt, secondInt : integer;
firstFloat, secondFloat : real;
firstChar, secondChar : char;
```
Given the input:
```   1234
456
7 8 9
```
1. What will be the value of each variable after the following statements ?
```   read(firstInt);

firstInt:      1234___
secondInt:     7______
firstFloat:    456____
secondFloat:   undefined
```
2. What will be the value of each variable after the following statements ?
```   read(firstInt);

firstInt:      1234___
secondInt:     7______
firstChar:     <CR>___
str:           456____
```
3. What will be the value of each variable after the following statements ?
```   read(firstChar);

firstInt:      234____
secondInt:     456____
firstChar:     1______
secondChar:    7______
```
4. What will be the value of each variable after the following statements ?
```   read(firstInt);