In this chapter we studied the record data structure. Records were shown to be useful for organizing a collection of related data items of different types. We were able to create some very general data structures to model our "real world" data organization through the use of hierarchical records.
In processing records, we learned how to reference each individual component through the use of a field selector consisting of the record variable name and field name separated by a period.
Each individual component of a record must be manipulated separately in an input or output operation or in an arithmetic expression. However, it is permissible to assign one record variable to another record variable of the same type (record copy statement), to compare two records of the same type for equality or inequality, and to pass a record as a parameter to a procedure or function.
This chapter also introduced a data structure called an array, a convenient facility for naming and referencing a collection of like items. We discussed how to declare an array type and how to reference an individual array element by placing a subscript in parentheses, following the array name.
The FOR
statement enables us to reference easily the elements
of an array in sequence. We used FOR
statements to initialize
arrays, to read and print arrays, and to control the manipulation of individual
array elements in sequence.
The new Ada constructs introduced in this chapter are described in Table 8.6.
Table 8.6
Summary of New Ada Constructs
Statement Effect
Record Declaration
SUBTYPE PartID IS Positive RANGE 1111.9999; TYPE Part IS RECORD A record type Part is declared ID : PartID; with fields that can store Quantity : Integer; two integers and a Price : Float; float number. Nuts and END RECORD; Bolts are record variables of type Part. Nuts, Bolts : Part;
Record Reference
TotalCost := Nuts.Quantity Multiplies two fields of Nuts. * Nuts.Price; Ada.Integer_Text_IO.Put(Item=>Bolts.ID); Displays ID field of Bolts.
Record Copy
Bolts := Nuts; Copies record Nuts to Bolts.
Record Aggregate Assignment
Bolts := (ID=>2234, Quantity=>53, Price=>0.09); Assigns values to all fields of Bolts.
Record Compare
IF Nuts = Bolts THEN Compares Nuts to Bolts.
Array Declaration
TYPE IntArray IS The data type ARRAY (1..10) OF Integer; describes an array with 10 type Integer elements. Cube Cube, Count : IntArray; and Count are arrays with this structure. SUBTYPE Index IS Integer RANGE 0..10; The data type Index is the range used as the subscript Name: ARRAY (Index) OF Character; type for Name, an array of characters.
Array Reference
FOR I IN 1 .. 10 LOOP Saves I3 in the Ith Cube(I) := I * I * I; element of array Cube. END LOOP; IF Cube(5) > 100 THEN Compares Cube(5) to 100. Ada.Integer_Text_IO.Put (Item=>Cube(1),Width=>5); Displays the first two cubes. Ada.Integer_Text_IO.Put (Item=>Cube(2),Width=>5);
Array Aggregate Assignment
Count := (3=>29,5=>17,OTHERS => 1); Sets all elements of Count to 1 except Count(3) and Count(5).
Array Copy
Count := Cube; Copies contents of array Cube to array Count.
Array Comparison
IF Count /= Cube THEN Compares each element of Countto the corresponding element of Cube. IF Count(1..5) = Cube(6..10) Compares slices of Count and Cube.
AStudent
is a variable whose type is the record type
declared below, provide a program segment that displays the initials of
AStudent
.
TYPE Student IS RECORD First: String(1..20); Last : String(1..20); Age: Natural; Score: Natural; Grade : Character; END RECORD;
Student
?
Integer
uses two bytes of storage and a character one,
how many bytes of storage are occupied by a variable of type
Student
?
Student
.
Ada.Text_IO.Put (Item=>AStudent.First(1));
Ada.Text_IO.Put (Item=>AStudent.Last(1));
PROCEDURE WriteStudent (OneStu : Student) IS BEGIN Ada.Text_IO.Put (Item=>"Student is "); Ada.Text_IO.Put (Item=>OneStu.First); Ada.Text_IO.Put (Item=>' '); Ada.Text_IO.Put (Item=>OneStu.Last); Ada.Text_IO.Put (Item=>"; age is "); Ada.Integer_Text_IO.Put (Item => OneStu.Age, Width=>1); Ada.Text_IO.Put (Item=>"; score is ");
Ada.Integer_Text_IO.Put (Item => OneStu.Score, Width=>1);
Ada.Text_IO.Put (Item=>"; grade is "); Ada.Text_IO.Put (Item=>OneStu.Grade); Ada.Text_IO.New_Line; END WriteStudent;
FOR
, sequential
Subscriber
, which contains
the fields Name
, StreetAddress
,
MonthlyBill
(how much the subscriber owes), and which paper the
subscriber receives (Morning
, Evening
, or
Both
).
Competition
declared below.
StringSize: CONSTANT Positive := 20; TYPE OlympicEvent IS RECORD Event: String(1..StringSize); Entrant: String(1..StringSize); Country : String(1..StringSize); Place : Integer END RECORD; Competition: OlympicEvent;
GPA
, Major
, Address
(consisting of
StreetAddress
, City
, State
, and
ZipCode
) and ClassSchedule
(consisting of up to six
class records, each of which has Description
, Time
,
and Days
fields). Use whatever data types are most appropriate for
each field.
PROCEDURE P IS TYPE AnArray IS ARRAY(1..8) OF Integer; X: AnArray; I: Integer; BEGIN FOR I IN 1..9 LOOP X(I) := I; END LOOP; END P;
b. When will the error be detected? What will the error be?
Week
that can be
referenced by using any day of the week as a subscript, where
Sunday
is the first subscript.
PROCEDURE P IS TYPE FloatArray IS ARRAY (Character) OF Float; X : FloatArray; I : Integer; BEGIN I := 1; X(I) := 8.384; END P;
PROCEDURE P IS TYPE FloatArray IS ARRAY (1..8) OF Float; X : FloatArray; I : Integer; BEGIN I := 1; X(I) := 8.384; END P;
X
of 20 integers with values from
0 to 100. Assume array X
already has values assigned to each
element.
FloatArray
) and an integer representing the length of the arrays.
The procedure copies the first array in the parameter list to the other array
in reverse order using a loop structure. Write the procedure.
N
data items into two arrays
X
and Y
of size 20. Store the product of
corresponding elements of X
and Y
in a third array
Z
, also of size 20. Display a three-column table showing the
arrays X
, Y
and Z
. Then compute and
display the square root of the sum of the items in Z
. Make up your
own data, with N
less than 20.
X = 1487625 Y = 12783 X = 60705202 Y = 30760832 X = 1234567890 Y = 9876543210(Hints: Store the numbers
X
and
Y
in two arrays X
and Y
of size 10, one
decimal digit per element (type Character
). If the number is less
than 10 digits in length, enter enough leading zeros (to the left of the
number) to make the number 10 digits long.
array X [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] 0 0 0 1 4 8 7 6 2 5array Y [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] 0 0 0 0 0 1 2 7 8 3
You will need a loop to add together the digits in
corresponding array elements, starting with the element with subscript 10.
Don't forget to handle the carry if there is one! Use a Boolean
variable Carry
to indicate whether or not the sum of the last pair
of digits is greater than 9.)
Satisfactory
. If the
score is more than 10 points higher than the average, assign the student a
grade of Outstanding
. If the score is more than 10 points below
the average, assign the student a grade of Unsatisfactory
.
(Hint: The output from your program should consist of a labeled
three-column list containing the name, exam score, and grade of each student.)
a. Count the number of households included in the survey and print a three-column table displaying the data read in. (You may assume that no more than 25 households were surveyed.)
b. Calculate the average household income, and list the identification number and income of each household that exceeds the average.
c. Determine the percentage of households having incomes below the poverty level. The poverty level income can be computed using the formula
p = $6500.00 + $750.00 (m - 2)
where m is the number of members of each household. This formula shows that the poverty level depends on the number of family members, m, and that the poverty level increases as m gets larger.
Test your program on the following data.
Identification number Annual income Household members 1041 $12,180 4 1062 13,240 3 1327 19,800 2 1483 22,458 8 1900 17,000 2 2112 18,125 7 2345 15,623 2 3210 3,200 6 3600 6,500 5 3601 11,970 2 4725 8,900 3 6217 10,000 2 9280 6,200 1
Copyright © 1996 by Addison-Wesley Publishing Company, Inc.