Previous topic |
Ada Home Page |
Index
Arrays of Records
An array item may be of any type
- can have an array with each item being a record
Purpose:
- often want to represent a collection of information
- a simple database of information on something
- each item has a number of attributes of interest => use a record
- need this information for many items => use an array of records
Example
Result list for a sporting event:
- several items of information about each competitor
- number, name, club, time to complete the event
- => record
- several competitors
max_field : constant := 50;
type Number is range 1 .. 1000;
subtype String20 is String(1..20);
type Time is digits 7 range 0.0 .. 600.0;
type Competitor is record
id_number : Number;
name : String20;
club : String20;
run_time : Time;
end record;
type Result_list is array (1 .. max_field) of Competitor;
race1, race2 : Result_list;
Using Arrays of Records
Declarations
- constants: subtype limits, array sizes
- elementary types: subtypes, enumeration types, field types
- record data type
- array type: array of that record type
- array variables
Referring to portions of the array:
- can refer to entire database (array of all records) for assignment,
as an actual parameter, etc
- can refer to one item (a record in the database)
- eg
race1
is an array of records, so
race1(J)
is a record
- eg
Display_Competitor(race1(J));
- can refer to a field of one item in database
race1(3).id_number := 23;
get (race1(I).id_number);
race1(J).name := "Joe Bloggs ";
PUT(race1(J).club);
GET(race1(next).run_time);
Examples
Two example programs show the design and code of programs that involve
arrays of records:
Previous topic |
Ada Home Page |
Index
c-lokan@adfa.oz.au / 27 Feb 96