Previous | Next | Table of Contents | Index | Program List | Copyright

8.1 Data Structures: Record Types

As described in the introduction, a record is a structure containing several fields, each field having its own type; this structure is described by a record type declaration. As is always the case with types, the record type declaration does not create any records; it is only a template or recipe for creating records.

Record Type Declaration

Before a record can be created or saved, the record format must be specified by means of a record type declaration.

Example 8.1

The staff of our small software firm is growing rapidly. To keep our records more accessible and organized, we decide to store relevant data, such as the descriptive information shown below, in an employee data base:

    ID  : 1234
    Name:  Caryn Jackson
    Gender : Female
    Number of Dependents: 3 
    Hourly Rate: 7.50 

Noting that the number of dependents should be of type Natural and the hourly rate and taxable salary should both be of type NonNegFloat, let us give an appropriate type declaration for each piece of information in the first three lines above:

    NameSize :CONSTANT Positive := 20;
    
    SUBTYPE IDType IS Positive RANGE 1111..9999;
    SUBTYPE NameType IS String(1..NameSize);
    TYPE GenderType IS (Female,Male);
We next declare a record type Employee to store this information. We must specify the name of each field and the type of information stored in each field. We choose the field names in the same way as we choose all other identifiers: The names describe the nature of the information represented.
    TYPE Employee IS RECORD
    	ID : IDType;
    	Name : NameType;
    	Gender : GenderType;
    	NumDepend : Natural;
    	Rate : NonNegFloat;
    END RECORD;
The record type is a template that describes the format of each record and the name of each individual data element. A variable declaration is required to allocate storage space for a record. The record variables Clerk and Janitor are declared next.
    Clerk : Employee;
    Janitor : Employee;
The record variables Clerk and Janitor both have the structure specified in the declaration for record type Employee. Thus, the memory allocated for each consists of storage space for six distinct values. Figure 8.1 shows the record variable Clerk, assuming the values shown earlier are stored in memory.

Figure 8.1
Record Variable Clerk

Figure 8.1

As illustrated in the type declaration for Employee, each of the fields of a record can be a predefined or user-defined type. There are no limitations on the type of a field, except that the type specification must be the name of a type that has already been declared. The record type declaration is described in the next display.

SYNTAX DISPLAY
Record Type Declaration

Form:
TYPE rec-type IS RECORD
	F1 : type1; 
	F2 : type2; 
	. 
	. 
	. 

	Fn : typen;
END RECORD; 

Example:
TYPE Fraction IS RECORD
	Numerator: Integer;
	Denominator: Positive;
END RECORD;

Interpretation:
The identifier rec-type is the name of the record structure being described. Each identifier Fi is a field name; the data type of each field Fi is specified by typei.

Note:
typei may be any predefined or user-defined type. Also, the field names must be unique: No two fields may have the same field name.

Manipulating Individual Fields of a Record

We can reference a record field by using a field selector, which consists of the record variable name followed by the field name. A period separates the field name and record name.

Example 8.2

The data shown in Fig. 8.1 could have been stored in Clerk through this sequence of assignment statements:

    Clerk.ID := 1234;
    Clerk.Name := "Caryn Jackson       ";   
    Clerk.Gender := Female;
    Clerk.NumDepend := 3; 
    Clerk.Rate := 7.50;

Once data are stored in a record, they can be manipulated in the same way as other data in memory.

The statements

    Ada.Text_IO.Put(Item => "The clerk is ");
    CASE Clerk.Gender IS
      WHEN Female =>
        Ada.Text_IO.Put (Item => "Ms. ");
      WHEN Male =>
        Ada.Text_IO.Put (Item => "Mr. "); 
    END CASE;
    Ada.Text_IO.Put (Item => Clerk.Name);
display the clerk's name after an appropriate title (Ms. or Mr.). For the data above, the output would be

The clerk is Ms. Caryn Jackson

Example 8.3

Program 8.1 computes the distance from an arbitrary point on the x-y plane to the origin (intersection of the x- and y-axes). The values of the x-coordinate and the y-coordinate are entered as data and stored in the fields X and Y of the record variable Point1. The formula used to compute the distance, d, from the origin to an arbitrary point (X, Y) is

      _________
d = \/ X2 + Y2
Each coordinate of the record variable Point1 is read separately. Ada.Text_IO does not provide operations to read an entire record; we will write our own operations to read records later in this chapter.

Program 8.1
Distance from Point to Origin

WITH Ada.Text_IO;
WITH Ada.Float_Text_IO;
WITH Ada.Numerics.Elementary_Functions;
USE  Ada.Numerics.Elementary_Functions;
PROCEDURE Distance_to_Origin IS
------------------------------------------------------------------------
--| Finds the distance from a point to the origin.   
--| Author: Michael B. Feldman, The George Washington University 
--| Last Modified: July 1995                                     
------------------------------------------------------------------------

  TYPE Point IS RECORD
    X : Float;
    Y : Float;
  END RECORD;

  Point1 : Point;              -- the data point   
  Distance : Float;            -- its distance to the origin   

BEGIN -- Distance_to_Origin   

  Ada.Text_IO.Put(Item => "Enter X coordinate (floating point) > ");  
  Ada.Float_Text_IO.Get(Item => Point1.X);
  Ada.Text_IO.Put(Item => "Enter Y coordinate (floating point) > ");  
  Ada.Float_Text_IO.Get(Item => Point1.Y);
  Distance := Sqrt(Point1.X ** 2 + Point1.Y ** 2);
  Ada.Text_IO.Put(Item => "Distance to origin is ");
  Ada.Float_Text_IO.Put(Item => Distance, Fore=>1,Aft=>2,Exp=>0);
  Ada.Text_IO.New_Line;

END Distance_to_Origin;
Sample Run
Enter X coordinate (floating point) > 3
Enter Y coordinate (floating point) > 4
Distance to origin is 5.00

Operations on Records

A type is always a set of values and a set of operations on those values. Now that we know how to declare record types, let us summarize the operations available for record values.

Four basic operations act on a record: store, retrieve, assignment, and equality test.

If Clerk and Janitor are both record variables of type Employee, the statement

    Clerk := Janitor; --copy Janitor to Clerk
copies each field of Janitor into the corresponding field of Clerk. It is also permissible to determine in a single statement whether two records R1 and R2 of the same record type are equal, that is, whether each field of R1 is equal to the corresponding field of R2. For example,
    IF R1 = R2 THEN
        DoSomething;
    ELSE
        DoSomethingElse;
    END IF;
executes DoSomething if R1 and R2 both contain the same field values, field by field, and executes DoSomethingElse otherwise.

Because arithmetic and logical operations--except for equality and inequality--can be performed on only individual memory cells, record variables cannot be used as the operands of predefined arithmetic and relational operators. These operators can be used only with individual fields of a record. However, we shall see in Chapter 10 that it is possible for a programmer to define arithmetic and relational operators on records.

Exercises for Section 8.1

Self-Check

  1. Each part in an inventory is represented by its part number, a descriptive name, the quantity on hand, and price. Define a record type Part.
  2. A catalog listing for a textbook consists of the author's name, title, publisher, and year of publication. Declare a record type CatalogEntry and variable Book and write assignment statements that store the relevant data for this textbook in Book.

Programming

  1. Modify program DistOrigin to find the distance between two points. Use the formula
                 _________________________
    Distance = \/( X1 - X2)2 + ( Y1 - Y2)2
    

    Store the points in two record variables of type Point


Previous | Next | Table of Contents | Index | Program List | Copyright

Copyright © 1996 by Addison-Wesley Publishing Company, Inc.