The subscript type of each array examined so far was a subrange of the integers. This, of course, is not required in Ada, because the subscript type can be any discrete type or subtype. A number of different array types are described in Table 8.4.
Table 8.4
Some Array Types and Applications
SUBTYPE NameLength IS Positive RANGE 1..10; TYPE NameArray IS ARRAY(NameLength) OF Character; Name : NameArray;
Name(1) := 'A';
SUBTYPE CelsiusRange IS Integer RANGE -10..10; TYPE TemperatureArray IS ARRAY(CelsiusRange) OF Float; Fahrenheit : TemperatureArray;
Fahrenheit(-10) := 14.0;
SUBTYPE UpperCase IS Character RANGE 'A'..'Z'; TYPE LetterCountArray IS ARRAY(UpperCase) OF Natural; LetterCount : LetterCountArray;
LetterCount('A') := 0;
SUBTYPE UpperCase IS Character RANGE 'A'..'Z'; TYPE LetterFoundArray IS ARRAY(UpperCase) OF Boolean; LetterFound : LetterFoundArray;
LetterCount('X') := False;
True
answers and False
answers to a quiz
TYPE AnswerArray IS ARRAY(Boolean) OF Natural; Answers : AnswerArray;
Answers(True) := 15;
The
array Name
has 10 elements and can be used to store the letters of
a person's name. The array Fahrenheit
has 21 elements and can be
used to store the Fahrenheit temperature corresponding to each Celsius
temperature in the range -10 though 10 degrees Celsius. For example,
Fahrenheit(0)
would be the Fahrenheit temperature, 32.0,
corresponding to 0 degrees Celsius. Arrays LetterCount
and
LetterFound
have the same subscript type (i.e., the uppercase
letters) and will be discussed in Example 8.14. The
array Answers
has only two elements with subscript values False
and
True
.
Example 8.13
The array MonthSales
, declared below, could be used to keep
track of the amount of sales in each month. The subscript type is
Simple_Dates.Months
, so the subscript values are the constants
Jan
to Dec
.
TYPE SalesArray IS ARRAY (Simple_Dates.Months) OF Float; CurrentMonth : Simple_Dates.Months; MonthSales : SalesArray; CurrentSales : Float;The element type of
SalesArray
is given as Float
, which
can be negative. This is appropriate because in an unusually bad month, the
value of returned goods can exceed that of newly sold goods, so the net sales
can be negative. The aggregate assignment
MonthSales := (OTHERS => 0.0);
initializes this array to all zeros. The statement
MonthSales(CurrentMonth) := MonthSales(CurrentMonth) + CurrentSales;
adds
the value of CurrentSales
to the element of
MonthSales
selected by the subscript CurrentMonth
.
Example 8.14
The arrays LetterCount
and LetterFound
described in
Table 8.4 have the subscript type
UpperCase
. Hence, there is an
array element for each uppercase letter. LetterCount('A')
could be
used to count the number of occurrences of the letter A
in a line;
LetterFound('A')
could be used to indicate whether or not the
letter A
occurs. If the letter A
occurs,
LetterFound('A')
would be True
; otherwise,
LetterFound('A')
would be False
.
Program
8.13
uses the arrays LetterCount
and LetterFound
described above to display the number of occurrences of each letter in a line
of text. The case of the letter is ignored (e.g., 't'
and
'T'
are considered the same letter). Only counts greater than 0
are printed.
Program 8.13
WITH Ada.Text_IO; WITH Ada.Integer_Text_IO; WITH Ada.Characters.Handling; PROCEDURE Concordance IS ------------------------------------------------------------------------ --| Finds and displays the number of occurrences of each letter. --| The case of each letter is immaterial. Letters with counts --| of zero are not displayed. --| Author: Michael B. Feldman, The George Washington University --| Last Modified: July 1995 ------------------------------------------------------------------------ Sentinel : CONSTANT Character := '.'; SUBTYPE UpperCase IS Character RANGE 'A'..'Z'; SUBTYPE LowerCase IS Character RANGE 'a'..'z'; TYPE LetterCountArray IS ARRAY (UpperCase) OF Natural; TYPE LetterFlags IS ARRAY (UpperCase) OF Boolean; LetterCount : LetterCountArray; -- array of counts LetterFound : LetterFlags; -- array of flags NextChar : Character; -- each input character BEGIN -- Concordance -- Initialize LetterCount LetterCount := (OTHERS => 0); -- Initialize counts LetterFound := (OTHERS => False); -- Initialize flags -- Read and process each data character. Ada.Text_IO.Put(Item => "Enter a line of text ending with a period."); Ada.Text_IO.New_Line; LOOP Ada.Text_IO.Get(Item => NextChar); -- Increment the count for this character, if it is a letter IF NextChar IN UpperCase THEN LetterCount(NextChar) := LetterCount(NextChar) + 1; LetterFound(NextChar) := True; ELSIF NextChar IN LowerCase THEN NextChar := Ada.Characters.Handling.To_Upper(NextChar); LetterCount(NextChar) := LetterCount(NextChar) + 1; LetterFound(NextChar) := True; END IF; EXIT WHEN NextChar = Sentinel; END LOOP; -- Display counts of letters that are in the line. Ada.Text_IO.New_Line; Ada.Text_IO.New_Line; Ada.Text_IO.Put(Item => "Letter Occurrences"); Ada.Text_IO.New_Line; FOR WhichChar IN UpperCase LOOP IF LetterFound(WhichChar) THEN Ada.Text_IO.Put(Item => " "); Ada.Text_IO.Put(Item => WhichChar); Ada.Integer_Text_IO.Put (Item => LetterCount(WhichChar), Width => 16); Ada.Text_IO.New_Line; END IF; END LOOP; END Concordance;Sample Run
Enter a line of text ending with a period. This is a test of the concordance program. Letter Occurrences A 3 C 3 D 1 E 3 F 1 G 1 H 2 I 2 M 1 N 2 O 4 P 1 R 3 S 3 T 4In Program 8.13, the array
LetterFound
is not really needed and was
included in the example mainly to show an application of an array of
Boolean
s. The condition
LetterFound(NextChar)could be written just as easily as
LetterCount(NextChar) > 0Writing the condition in this way would eliminate the need for the second array.
IndexType
is a
subtype of Integer
with range -5..5
:
a. ARRAY (1..20) OF Character;
b. ARRAY ('0'..'9') OF Boolean;
c. ARRAY(IndexType) OF Float;
d. ARRAY (Boolean) OF Character;
e. ARRAY (Character) OF Boolean;
a. The areas associated with each room in a group of rooms (living room, dining room, kitchen, etc.).
b. The number of students in each grade of an elementary school.
c. A letter associated with each color in a collection of colors. This letter will be the first letter of the color name.
Copyright © 1996 by Addison-Wesley Publishing Company, Inc.