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:

RangeGrade
90 <= Average <= 100A
75 <= Average < 90B
60 <= Average < 75C
50 <= Average < 60D
Average < 50F
Sample run:
   Enter average: 82.3
   Grade: B
Hints, hints, hints:

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;                                 
                                                   
   VAR grade : real;                               
      letter : char;                               
                                                   
   BEGIN                                           
                                                   
      write('Enter average: ');                    
      readln(grade);                               
      IF (grade >= 90) AND (grade <= 100) THEN     
         letter := 'A'                             
      ELSE                                         
         IF grade >=75 THEN                        
            letter := 'B'                          
         ELSE                                      
            IF grade >= 60 THEN                    
               letter := 'C'                       
            ELSE                                   
               IF grade >= 50 THEN                 
                  letter := 'D'                    
               ELSE                                
                  letter := 'F';                   
                                                   
      writeln('Grade: ', letter);                  

   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:

CommandProcedure
A or aAddDB(error)
D or dDeleteDB(error)
U or uUpdateDB(error)
L or lListDB(error)
P or pPrintDB(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                                                
        'A', 'a' : AddDB(error);                                     
        '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:

Sample run:
 
   Enter the weight in ounces: 3.4
   The 3.4-ounce postage charge is $ 1.26
Notes: 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: ');                                  
      readln(weight);                                                         
                                                                              
      { Compute the weight to the next greater integer }                      
                                                                              
      intWeight := round(weight + 0.49);                                      
                                                                              
      { Price is $ 0.33 for first ounce, $ 0.31 for other ounces }            
                                                                              
      price := FIRSTOUNCE + (intWeight - 1) * NEXTOUNCES;                     
                                                                              
      writeln('The ', weight:0:1,'-ounce  postage charge is $ ', price:0:2);  
                                                                              
   END.