Chapter 8

Answers to Exam Preparation Exercises

1.     True.

2.     True

4.     True.

5.     True.

6.     Definitions:

variable parameter           a parameter through which the address of the actual parameter is passed to the formal parameter, causing the formal parameter to become a synonym for the actual (thus allowing any changes made to that parameter in the procedure to be returned to the calling block).

 

value parameter               a parameter through which the value of the actual parameter is copied into the formal parameter of a procedure; if the procedure changes the formal parameter, the effect is strictly localized (not returned to the block that called the procedure).

 

local variable                   a variable that is active and usable only within a block.

 

global variable                 a variable declared in the main program.

 

nonlocal access                access from inside a nested block to an outside variable that is not passed through the parameter list (and is not defined locally).

 

scope                               the parts of a program to which a declaration is visible.

 

side effects                      any communication between modules that is not part of the explicitly designed interface between them (for example, use of global variables, or modification in a module of a parameter that should be a value rather than a VAR parameter).

 

name precedence             the priority treatment accorded to a name that is local to a procedure over a nonlocal name with the same spelling.

7.     S equals 5 and T equals 20 within procedure One.

        D equals 5 and E equals 14 after the first call to One.

        S equals 5 and T equals 20 within procedure One.

        D equals 12 and E equals 5 after the second call to One.

9.     Scope diagram for exercise 8:

10.   Output from program Params.

X equals 3 after the call to Refer.

X equals 16 after the call to Value.

X equals 17 after the call to Local.

X equals 7 after the call to Global.

12.   Output from program Scope_Rule.

X equals 3 after the call to Sub_A.

X equals 6 after the call to Sub_B.

X equals 14 after the call to Sub_C.

13. Output from program More_Scope_Rule.

X equals 3 after Sub_A is called.

X equals 14 after Sub_B is called.

X equals 9 after Sub_C is called.

Answers to Programming Warm-Up Exercises

2.     Procedure Get_Mean_Of:

PROCEDURE Get_Mean_Of (    HowMany:

                             Integer;

                       VAR Mean:

                             Real);

 

(* Assumption:  HowMany > zero *)

 

VAR

  Count:

    Integer;

  Total,

  Number:

    Real;

 

BEGIN (* Get_Mean_Of *)

  Total := 0.0;

  Count := 1;

  WHILE Count <= HowMany DO

    BEGIN

      Readln(Number);

      Total := Total + Number;

      Count := Count + 1

    END;

  Mean := Total / HowMany

END;  (* Get_Mean_Of *)

3.     Procedure Max_Value:

PROCEDURE Max_Value(    Integer1,

                        Integer2,

                        Integer3:

                          Integer;

                    VAR Largest:

                          Integer);

 

BEGIN (* Max_Value *)

  IF Integer1 >= Integer2

    THEN

      IF Integer1 >= Integer3

        THEN

          Largest := Integer1

        ELSE

          Largest := Integer3

    ELSE

      IF Integer2 >= Integer3

        THEN

          Largest := Integer2

        ELSE

          Largest := Integer3

END;  (* Max_Value *)            

4.     Procedure Compute_Distance:

PROCEDURE Compute_Distance (    X1,

                                Y1,

                                X2,

                                Y2:

                                  Real;

                            VAR Distance:

                                  Real);

 

BEGIN (* Compute_Distance *)

  Distance := Sqrt(Sqr(X2 - X1) + Sqr(Y2 - Y1))

END;  (* Compute_Distance *)

7.     Procedure Add_Time:

PROCEDURE Add_Time (VAR Hours,

                        Minutes:

                          Integer;

                        ElapsedTime:

                          Integer);

 

BEGIN (* Add_Time *)

  Minutes := Minutes + Elapsed Time;

  Hours := Hours + Minutes DIV 60;

  Minutes := Minutes MOD 60

END;  (* Add_Time *)

9.     Program using Acronym as a procedure:

PROGRAM CallAcronym (Input, Output);

 

PROCEDURE Acronym ;

 

CONST

  Blank = ' ';

 

VAR

  Ch:

    Char;

 

  PROCEDURE SkipBlanks (VAR Ch:

                            Char);

  BEGIN (* SkipBlanks *)

    Read(Ch);

    WHILE Ch = Blank DO

      Read(Ch)

  END;

 

  PROCEDURE SkipNonBlanks (VAR Ch:

                               Char);

 

  BEGIN (* SkipNonBlanks *)

    Read(Ch);

    WHILE Ch <> Blank DO

      Read(Ch)

  END;  (* SkipNonBlanks *)

 

BEGIN (* Acronym *)

  WHILE NOT EOLN DO

    BEGIN

      SkipBlanks(Ch);

      Write(Ch);

      SkipNonBlanks(Ch)

    END;

  Readln;

  Writeln

END;  (* Acronym *)

 

BEGIN (* CallAcronym *)

  WHILE NOT EOF DO

    Acronym

END.  (* CallAcronym *)

10.   Rewrite of Program SideEffects without globals. The names no longer reflect what is happening, so perhaps the program and the procedure should be renamed NoSideEffects and MashParameters!

PROGRAM SideEffects (Input, Output);

 

  PROCEDURE MashGlobals (VAR A,

                             B,

                             C:

                               Integer);

 

  VAR

    Temp:

      Integer;

 

  BEGIN (* MashGlobals *)

    Temp := A + B;

    A := B + C;

    B := Temp

  END;  (* MashGlobals *)

 

BEGIN (* SideEffects *)

  Readln(A, B, C);

  MashGlobals(A, B, C);

  Writeln('A= ', A:1, ' B= ', B:1, ' C= ',C:1)

END.  (* SideEffects *)

11.   The syntax error is the omission of VAR before InFile in the procedure heading.  Text variables must be passed as VAR parameters.  The incorrect output is caused by the use of a priming read before the WHILE NOT EOLN loop and a Read as the last action inside that loop.  The last character on each line is never written to OutFile.  Corrected procedure:

PROCEDURE FileCopy(VAR Infile:

                         Text;

                   VAR Outfile:

                             Text);

VAR

  Ch:

    Char;

 

BEGIN (* FileCopy *)

  Reset(InFile);

  Rewrite(OutFile);

  WHILE NOT EOF(InFile) DO

    BEGIN

      WHILE NOT EOLN(InFile) DO

        BEGIN

          Read(InFile, Ch);

          Write(OutFile, Ch)

        END;

      Readln(InFile);

      Writeln(OutFile)

    END

END;  (* FileCopy *)