The subscript type for each dimension of the multidimensional array
TicTacToe
is a subrange of type Integer
. It is not
necessary for the subscript types to all have the same base type. The arrays in
the next example have a different subscript type for each dimension.
Example 12.7
A university offers 50 courses at each of five campuses. The registrar's
office can conveniently store the enrollments of these courses in the array
Enroll
declared below.
MaxCourse : CONSTANT Positive := 50; -- maximum number of courses SUBTYPE Course IS Positive RANGE 1..50; TYPE Campus IS (Main, Ambler, Center, Delaware, Montco); TYPE CourseByCampus IS ARRAY (Course, Campus) OF Natural; Enroll : CourseByCampus;
This
array consists of 5 x 50 = 250 elements, as shown in
Figure
12.4. Enroll(1, Center)
represents the number of students in
course 1
at Center
campus.
Figure 12.4
Two-Dimensional Array Enroll
If the registrar wanted to break down this enrollment information according to student rank, a three-dimensional array with 1000 elements would be required. This array is declared below and shown in Figure 12.5.
MaxCourse : CONSTANT Positive := 50; -- maximum number of courses SUBTYPE Course IS Positive RANGE 1..50; TYPE Campus IS (Main, Ambler, Center, Delaware, Montco); TYPE Rank IS (Freshman, Sophomore, Junior, Senior); TYPE CourseByCampusByRank IS ARRAY (Course, Campus, Rank) OF Natural; ClassEnroll : CourseByCampusByRank; Total : Natural; -- student totals
The
subscripted variable ClassEnroll(1, Center, Senior)
represents the
number of seniors taking course 1
at Center
campus.
Figure 12.5
ClassEnroll
Example 12.8
We can again use aggregates to initialize the entire three-dimensional array to zero, which would need to be done at the beginning of a university registration cycle, for example.
ClassEnroll := (OTHERS => (OTHERS => (OTHERS => 0)));
Example 12.9
The program segment
Total := 0; FOR ClassRank IN Rank LOOP Total := Total + ClassEnroll(1, Center, ClassRank); END LOOP;
computes
the total number of students of all ranks in course 1
at
Center
campus. The program segment
Total := 0; FOR CurCampus IN Campus LOOP FOR ClassRank IN Rank LOOP Total := Total + ClassEnroll(1, CurCampus, ClassRank); END LOOP; END LOOP;
computes
the total number of students in course 1
(regardless of rank or
campus). Finally, the total enrollment is computed by the program segment
Total := 0; FOR CurCourse IN Courses LOOP FOR CurCampus IN Campus LOOP FOR ClassRank IN Rank LOOP Total := Total + ClassEnroll(CurCourse,CurCampus, ClassRank); END LOOP; END LOOP; END LOOP;
Math1
,
Algebra
, Geometry
, Algebra2
,
Trigonometry
, Calculus
) at your old high school
according to the grade level and gender of the students. How many elements are
in this array?
ClassEnroll
might be stored in row-major form. What would be the
offset for the array element ClassEnroll(1,Center,Senior)
and the
general formula for ClassEnroll(i,j,k)
?MaxCourse
as 5 and write program segments that
perform the following operations:
a. Enter the enrollment data.
b. Find the number of juniors in all classes at all campuses. Students will be counted once for each course in which they are enrolled.
c. Find the number of sophomores on all campuses who are enrolled in course 2.
d. Compute and display the number of students at Main
campus
enrolled in each course and the total number of students at Main
campus in all courses. Students will be counted once for each course in which
they are enrolled.
e. Compute and display the number of upper-class students in all courses at each campus, as well as the total number of upper-class students enrolled. (Upper-class students are juniors and seniors.) Again, students will be counted once for each course in which they are enrolled.
Copyright © 1996 by Addison-Wesley Publishing Company, Inc.