Chapter 12

Answers to Exam Preparation Exercises

1.     (a) Read   (c) EOF   (d) Write

3.     One component type may be collected in one file.

4.     Read(N), where N is a Real or Integer variable.

8.     False; the indices must be ordinal, but the components can be of any type.

9.     Code fragment:

TYPE

  Score_Range = 0..100;                           (* a *)

  Index_Range = 1..Max_Length;

  Student_Scores = ARRAY[Index_Range] OF

                     Score_Range;                 (* b *)

 

VAR

  Quiz_One: Student_Scores;                       (* c *)

10.   (a)

TYPE

  Bird_Type = (Cardinal, Blue_Jay, Humming_Bird, Robin);

        (b)

Siting_Type = ARRAY[Bird_Type] OF Integer;

        (c)

VAR Sitings: Siting_Type;

13.   Output from the program:

    11    15    -4

    19    14     5

     4     2     2

    17     6    11

     1     3    -2

 

    52    40    12

14.   Array variable declarations.

(a)    24-component Real array.

TYPE

  IndexRange = 1..24;

  RealType = ARRAY[IndexRange] OF Real;

 

VAR

  RealArray:

    RealType;

(b)   24-component Integer array.

TYPE

  IndexRange = 24..47;

  IntegerType = ARRAY[IndexRange] OF Integer;

 

VAR

  IntegerArray:

    IntegerType;

(c)    26-component array indexed by letters.

TYPE

  IndexRange = 'A'..'Z';

  BooleanType = ARRAY[IndexRange] OF Boolean;

 

VAR

  BooleanArray:

    BooleanType;

(d)   10-component Char array.

TYPE

  IndexRange = -10..-1;

    CharType = ARRAY[IndexRange] OF Char;

 

VAR

  CharArray:

    CharType;

17.   Array elements are accessed directly by an index which gives the element's position within the array.  File elements are accessed sequentially by a procedure or directly by a procedure which gives the file element's position in the file.  All of the array elements are resident in memory; only one file element is in memory at a time.

18.   (a) invalid   (b) invalid   (c) valid   (d) invalid   (e) valid   (f) valid    (g) valid

19.   (a) A_Ref := Guide[71].Chart

(b) A_Code := Guide[88].Chart.Token[1]

(c) Guide[94].Chart.Token[23,1] := 'X'

(d) A_Ref.Symbol[20,9] := A_Map.Map_Code[4]

Answers to Programming Warm-Up Exercises

3.     Declaration of a binary file.

TYPE

  Real_File = FILE OF Real;

4.     SeekEOF(Skipped)

5.     Function that checks whether the next two records are identical.

FUNCTION Ident(VAR File1, File2: Data_Type): Boolean;

(* Pre:  Files are open for reading *)

VAR

  File1_Rec, File2_Rec : Rec_Type;

BEGIN

  Read(File1, File1_Rec);

  Read(File2, File2_Rec);

  Ident := File1_Rec = File2_Rec

END;

8.     Procedure to check for Score less than 60.

PROCEDURE CheckScore (VAR Failing:

                            FailType;

                          Score:

                            ScoreType;

                          ListLength:

                            IndexType);

 

VAR

  Counter:      (* Loop control variable *)

    IndexType;

 

BEGIN  (* CheckScore *)

  FOR Counter := 1 TO ListLength DO

    IF Score[Counter] < 60

      THEN

        Failing[Counter] := True

END;  (* CheckScore *)

9.     Procedure to check for a passing score.

PROCEDURE Check_Score (VAR Passing: Fail_Type;

                           Score: Score_Type;

                           List_Length: Index_Type);

VAR

  Counter:  Index_Type;

 

BEGIN  (* Check_Score *)

  FOR Counter := 1 TO List_Length DO

    IF Score[Counter] >= 60

      THEN  Passing[Counter] := True

END;  (* Check_Score *)

10.   Function that returns number of passes.

FUNCTION TallyPass (Passing:

                      PassType;

                    ListLength:

                      IndexType):

                        Integer;

VAR

  Counter:         (* Loop control variable *)

    IndexType;

  Tally:

    O..MaxLength;

 

BEGIN  (* TallyPass *)

  Tally := 0;

  FOR Counter := 1 TO ListLength DO

    IF Passing[Counter]

      THEN

        Tally := Tally + 1;

  TallyPass := Tally

END;  (* TallyPass *)

11.   Function that checks for both passing and failing.

FUNCTION Error (Passing: Pass_Type;

                Failing: Fail_Type;

                List_Length: Index_Type): Boolean;

 

VAR

  Counter: 1..MaxLength + 1;

  Error_Found: Boolean;

 

BEGIN  (* Error *)

  Counter := 1;

  Error_Found := False;

  WHILE (Counter <= List_Length) AND NOT Error_Found DO

    IF Passing[Counter] <> Failing[Counter]

      THEN  Counter := Counter + 1

      ELSE  Error_Found := True;

  Error := Error_Found;

END;  (* Error *)

14.   Output as the code is written:

BCDE<eoln>    The first and last characters are missing.

GHIJ<eoln>    The last character is missing.

LMNO<eoln>    The last character is missing.

<eof>

        Corrected code:

Reset(In1);

Rewrite(Out1);

WHILE NOT EOF(In1) DO

  BEGIN               (* priming read has been removed *)

    WHILE NOT EOLN(In1) DO

      BEGIN

        Read(In1, Character);   (* read before writing *)

        Write(Out1, Character);

      END;

    Readln(In1);

    Writeln(Out1);

  END;

15.   Procedure to copy Input to a binary file:

TYPE

  Bin_File = FILE OF Integer;

VAR

  Data : Bin_File;

 

PROCEDURE Make_Bin_File (VAR Data: Bin_File);

 

VAR

  Number: Integer;

 

BEGIN (* Make_Bin_File *)

  Rewrite(Data);

  Writeln('Enter one integer per line.');

  Writeln('Signal end of file when done.');

  WHILE NOT EOF DO

    BEGIN

      Readln(Number);

      Write(Data, Number)

    END;

END;  (* Make_Bin_File *)

16.   Program segment to read a set of part numbers:

CONST

  Number_Of_Parts = 100;

 

TYPE

  Part_Type = RECORD

                Number : Integer;

                Price : Real

              END;  (* Record *)

  Part_Index = 1..Number_Of_Parts;

  Part_Range = 0..Number_Of_Parts;

  Parts_Type = ARRAY[Part_Index] OF Part_Type;

VAR

  Parts: Parts_Type;

  Temp_Part: Part_Type;

  List_Length, Index: Part_Range;

 

BEGIN

  List_Length := 0;

  WHILE NOT EOF DO

    BEGIN

      Readln(Temp_Part.Number, Temp_Part.Price);

      List_Length := List_Length + 1;

      Parts[List_Length] := TempPart

    END;

END;