Chapter 11

Answers to Exam Preparation Exercises

2.     Valid and invalid set expressions:

(a) 'A' IN Digits                                                      Valid

(b) ['X','Y','Z']  >= ['N'..'Y']                                    Valid

(c) Digits / ['1']                                                        Invalid; / is illegal

(d) Digits * ['1']                                                       Valid; * means intersection

(e) ['1'] + ['2'] + ['3'] = ['1'..'3']                                Valid

(f) ['1'..'9'] >= ['0'..'9'] - ['3'..'7']                              Valid

(g) 4 IN Digits                                                        Invalid; 4 is an integer

4.     Definitions:

record:                                     a nonhomogeneous unstructured data type in which the components are accessed by field name

field identifier:                         the name of a component in a record

field selector:                           the expression used to access a field of a record variable; made up of the name of the record variable and the field identifier, separated by a period

hierarchical record:                  a record that contains another record as a component

5. False

6.     Identify an appropriate data structure:

(a) hierarchical record containing strings   (b) record containing strings  (c) record  (d) enumerated type or strings

7.     Values in records.

Today.Month = 1

Today.Day = 1

Today.Year = 1995

A_Name = '               '

Friend.First_Name = '               '

Friend.Last_Name = '               '

Friend.Birth_Date.Month = 1

Friend.Birth_Date.Day = 1

Friend.Birth_Date.Year = 1995

Self.First_Name = '               '

Self.Last_Name = '               '

Self.Birth_Date.Month = 1

Self.Birth_Date.Day = 1

Self.Birth_Date.Year = 1995

8.     Friend and Self are both of type Person. Because Self is the inner scope, the fields of Friend can never be accessed.

Answers to Programming Warm-Up Exercises

2.     Set operations:

(a)

TYPE

  UP_Letters_Type = SET OF 'A'..'Z';

VAR

  Set_A, Set_B, Set_C: UP_Letters_Type;

(b)   Set_A := ['A'..'N'];

(c)    Set_B := ['K'..'Z'];

(d)   Show contents of Set_C:

     Set_A + Set_B:     ['A'..'Z']

     Set_A - Set_B:      ['A'..'J']

     Set_A * Set_B:      ['K'..'N']

     Set_B - Set_A:      ['O'..'Z']

(e) Evaluate expressions:

      Set_A <> Set_B                      True; expression is  ['A'..'Z'] <> ['K'..'Z']

      Set_A <= Set_B                      False; expression is  ['A'..'Z'] <= ['K'..'Z']

      Set_A * Set_B >= Set_A        False; expression is ['K'..'N'] <=  ['A'..'N']

      Set_A + Set_B >= Set_A       True; expression is ['A'..'Z'] >= ['A'..'N']

5.     Hierarchical record definition

CONST

  Name_Length = 20;

  Max_Courses = 50;

 

TYPE

  Course_Index = 1..Max_Courses;

  Name_String = String[Name_Length];

  Date_Type = RECORD

                Month : 1..12;

                Year  : 1950..2020

              END; (* Record *)

  Class_Type = (Freshman, Sophomore, Junior, Senior);

  Grade_Type = (A, B, C, D, F, Q):

  Student_Type = RECORD

                   Name : Name_String;

                   Student_ID : Integer;

                   Hours_To_Date : Integer;

                   Courses_To_Date : 0..Max_Courses;

                   First_Enrolled : Date_Type;

                   Class : Class_Type;

                   GPA : Real

                 END;  ( Record )

6. Record type for an apartment locator service:

(a)

CONST

  Max_String = 20;

TYPE

  A_String = String[Max_String];

  Apt = RECORD

          Landlord,

          Address : A_String;

          Bedrooms : Integer;

          Price : Real

        END;  (* Record *)

(b)

VAR

  An_Apt : Apt;

(c)

PROCEDURE Read_Record (VAR For_Lease: Apt);

BEGIN  (* Read_Record *)

  WITH For_Lease DO

    BEGIN

  Readln(Landlord);

      Readln(Address);

      Readln(Bedrooms);

      Readln(Price)

    END

END;   (* Read_Record *)