V22.0002.002 - SPRING 2000, Midterm 1

1. (15 points)
 I. Write the following as a boolean expression. Letter is 'V' or Goals is less than 4. (Letter = 'V') OR (Goals < 4) II. Write the following as a boolean expression. State is neither High nor Low. (State <> High) AND (State <> Low) III. Evaluate the following: (3 * 5 <= 11) AND (16 DIV 4 = 1) FALSE IV. Evaluate the following: NOT (ord('a') <= ord('z') - 10) OR TRUE TRUE V. Evaluate the following: ((33 MOD 5) IN [1..2,6..1]) AND FALSE FALSE

2. (15 points) Write a program that takes a student's point average and returns the equivalent letter grade. The equivalence between point average and letter grade is given by the following table:

 Range Grade 90 <= Average <= 100 A 75 <= Average < 90 B 60 <= Average < 75 C 50 <= Average < 60 D Average < 50 F
Sample run:
```   Enter average: 82.3
```
Hints, hints, hints:
• You are comparing reals. Can you use the CASE statement ?

Extra credit (5 points) Making a program robust means to guard against possible errors. What would you do to make your program robust ?

```   PROGRAM Grader;

letter : char;

BEGIN

write('Enter average: ');
letter := 'A'
ELSE
letter := 'B'
ELSE
letter := 'C'
ELSE
letter := 'D'
ELSE
letter := 'F';

END.
```
Extra credit (5 points): Print error message if user enters average greater than 100 or less than 0.

3. (20 points) You are part of a team writing a database management program. You're in charge of writing the procedure that evaluates the commands. This procedure is called ParseCommand. The procedure has two parameters:

1. command, a 1-character variable that identifies the current command (see below).
2. error, an integer that returns to the caller the number of errors that have been encountered.
For each command below, there is an associated procedure that executes the command. All of these procedures take one parameter: error, the number of errors encountered while executing the command. All of these procedures have already been written.

List of commands and associated execution procedure:

 Command Procedure A or a AddDB(error) D or d DeleteDB(error) U or u UpdateDB(error) L or l ListDB(error) P or p PrintDB(error)
Your procedure must be able to handle both uppercase and lowercase commands. The procedure must also detect invalid commands. You are writing only the procedure ParseCommand, no main or any of the other procedures.

```   PROCEDURE ParseCommand(command : char; VAR error : integer);

BEGIN

CASE command OF
'D', 'd' : DeleteDB(error);
'U', 'u' : UpdateDB(error);
'L', 'l' : ListDB(error);
'P', 'p' : PrintDB(error);
ELSE
error := error + 1;
END; { case }

END; {ParseCommand}
```

4. (15 points) What's the output of the following procedure ?

```   PROGRAM Quiz;
VAR count : integer;
letter : char;
CONST SENTINEL = '\$';
BEGIN

count := 1;
letter := ' ';
WHILE (count < 6) AND (letter <> SENTINEL) DO
BEGIN

CASE letter OF
' ' : letter := 'a';

'a'..'l':
BEGIN
letter := chr(ord(letter)+1);
count := count + 1;
END;

'm'      : letter := SENTINEL;
END; { case }

write(letter);

END; { while }

writeln;
writeln(count, ' letters were printed.');
END.
```

```     abcdef
6 letters were printed.
```

5. (20 points) The following code contains at least 5 syntax errors. Identify the line number and the error.

``` 1. PROGRAM Quiz;

2. PROCEDURE Dummy(VAR counter : integer, command : char );
3. BEGIN
4.    CASE command DO
5.      'a' : counter := counter + 1;
6.      's' : counter := counter - 1;
7.    END; { case }
8. END; { Dummy}

9. VAR dummy : integer;
10. CONST Dumm = 3;
11. BEGIN

12.    dummy := 1;
13.    WHILE NOT dummy = 10 DO
14.    BEGIN
15.       Dummy(dummy, 'a');
16.       writeln('Current value: ', dummy);
17.       Dummy(Dumm, 's');
18.    END;

19. END.
```
 Number Line Error 1. 2 The type declarator integer should be followed by a semi-colon, not a comma. 2. 4 The CASE statement uses the keyword OF, not DO. 3. 9 Duplicate identifier dummy. 4. 13 There should be parenthesis around the expression dummy = 10. 5. 17 The first parameter of the procedure Dummy is a variable parameter, but Dumm is a constant.

6. (15 points) Write a program to compute the postage charge for a first class letter. The user will enter the letter's weight in ounces as a real. To compute the postage use the following data:

• First ounce, \$ 0.33
• Each additional ounce, \$ 0.31
Sample run:
```
Enter the weight in ounces: 3.4
The 3.4-ounce postage charge is \$ 1.26
```
Notes:
• To compute the correct price, you have to convert the weight expressed as a real number to the next highest integer. To accomplish this you can use the following formula: round(weight + 0.49).
• The output must be formated appropriately: i.e., one decimal digit for the weight, two decimal digits for the price.
Extra credit (5 points) Making a program robust means to guard against possible errors. What would you do to make your program robust ?

```   PROGRAM Mail;

VAR weight, price : real;
intWeight      : integer;

CONST FIRSTOUNCE = 0.33;
NEXTOUNCES    = 0.31;

BEGIN

write('Enter the weight in ounces: ');