< previous page page_797 next page >

Page 797
parallel row represents the information for one entry. This set of structures is shown in Figure 14-12.
A parallel organization of seven different arrays seems to be a clumsy way of representing this logical structure. Yet this is the structure that you have to use for this type of problem if your programming language does not have record data typesand some do not. However, in C++, it makes more sense to define an entry using a record (struct) data type.
Let's look now at a data structure in which an array of structs is used to represent the address book. The following declarations describe this data structure.
const int MAX_FRIENDS = 150;     // Maximum number of friends

typedef char String8[9];         // Room for 8 characters plus \0
typedef char Stringl5[16];       // Room for 15 characters plus \0

struct EntryType
{
    String15 firstName;
    Stringl5 lastName;
    int      areaCode;       // Range 100..999
    String8  phoneNumber;
    int      month;          // Range 1..12
    int      day;            // Range 1..31
    int      year;           // Range 1900..2100
};

EntryType addressBook[MAX-FRIENDS];
firstName, lastName, areaCode, phoneNumber, month, day, and year are member names within the struct type EntryType. The members firstName and lastName are strings of type Stringl5, areaCode is an integer, and phoneNumber is a string of type String8. Phone numbers have only seven digits, but we decided to include the hyphen between the first three digits and the last four digits because this is how phone numbers are usually printed. The members month, day, and year are integers. A complete entry with values stored in the struct variable addressBook[0] is shown in Figure 14-13.
Notice that EntryType is a flat (nonhierarchical) structure. For birth dates, we could have used a lower-level DateType struct as we did earlier in the MachineRec type. Also, the two pieces of a phone numberthe area code and the numbermight reasonably be defined in a lower-level struct type. The second Problem-Solving Case Study in this chapter uses just such a phone number type as part of a hierarchical record.

 
< previous page page_797 next page >