Instructor's Manual for

M.B. Feldman and E.B. Koffman, Ada 95 Problem Solving and Program Design, 3rd edition. Copyright 1999, Addison-Wesley Publishing Company. All Rights Reserved.

Questions and comments to mfeldman@seas.gwu.edu.


Chapter 13 Multidimensional Arrays and Variant Records

last revised March 1999

Chapter Objectives

The student will

  1. learn to declare and use multidimensional arrays
  2. learn to declare and use variant records.

New Terms

Section 13.1

multidimensional array

row-major order

offset

column-major order

Section 13.4

variant record

discriminant

constrained

unconstrained

Notes and Suggestions

Section 13.1

Take the time to go through the details of the underlying row-major representation of a 2-dimensional array. You can point out that the Ada standard does not usually specify underlying representations, and in this case does not specify row- or column-major order, or some other representation. In practice, Ada compilers usually use row-major representation, because this has become traditional with other languages (except for FORTRAN, which has always specified a column-major representation).

This is a good example of information-hiding: the subscripting syntax allows one to work with multidimensional arrays without having to be aware of their underlying storage layout.

Section 13.2

This section reinforces the previous one by extending the 2-dimensional case to three dimensions. Extending row-major order to three dimensions is a good exercise for students to hone their understanding of storage layouts. Be sure to go over the nested loop examples; the self-check exercises provide more opportunity for this.

Section 13.3

This section suggests a project to modify the spider ADT package to account for multiple spiders in the room. A 2-dimensional Boolean array is used to represent the room, with each cell indicating the presence of absence of a spider. The student is asked to modify any necessary operations. A new exception, Hit_a_Spider, is introduced in case a spider tries to enter an occupied square.

Section 13.4

Variant records in Ada are designed to be safe, and cannot be used to defeat the type system. More formally, variant records cannot be used in Ada to create "free unions," as is possible in Pascal and C. A variant record must have a tag field, which is called a discriminant in Ada terminology. (By the way: Ada's mechanism for getting the effect of a free union is called Unchecked_Conversion and is not covered in this book.)

It is worth spending time explaining constrained and unconstrained variant records, and the rules on p. 572 ff. governing just when, and how, the value of a discriminant can be altered.

Section 13.5

This case study shows an example of unconstrained variant records. A variable can hold different geometric shapes at different times. In my experience, the most difficult aspect of variant records is reading a variant record from the keyboard or a file. Because of the rules governing discriminants, it is not immediately obvious how to do it, and student attempts often lead to compilation and runtime errors. The child package Geometry.IO shows how to read a variant record value from the keyboard. If you assign a project using variant records, be sure to explore this package with your students. Focus especially on why it is not feasible to read all the values directly into the fields of the output parameter!