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
last revised March 1999.
The student will
composite type |
type constructor |
data base |
field selector |
record |
record field |
record aggregate assignment |
|
hierarchical record |
|
array |
array type declaration |
array subscript |
array element |
subscripted variable |
array aggregate assignment |
array index |
|
menu |
sequential access |
random access |
|
partial array |
|
Operations on structured types in Ada are more uniform and general than they are in other languages: assignment and equality testing are predefined for compatible arrays and records, and records and arrays may be returned as function results. This generality might even appeal to students with experience in other languages.
In this chapter we introduce records before arrays so that students find arrays of records as natural as arrays of scalars.
I generally motivate this chapter by summarizing control structures in terms of "grouping": we group statements into control structures, control structures into subprograms, and subprograms into packages. Composite data structures represent a similar grouping of data elements, and the two kinds of grouping work together very well and naturally.
A useful analogy for records and arrays is a box of index cards, or a "rolodex." A single rolodex card contains several types of information about an acquaintance: name, address, phone number, e-mail address, birth date. This serves as a structured record of these facts about an individual. A rolodex file is a set of these cards. A single card is like a program record; a rolodex file is like an array of these records.
Point out the advantage of records in grouping together logically related information of different types. There are countless real-world examples of this to which students can relate. Students with Fortran or BASIC experience may have some difficulty relating to records at first.
Emphasize that a record type declaration declares no variables and allocates no storage; it is merely a template.
Ada does not restrict the type of subprogram parameters or function results, so parameter passing is completely general.
Aggregate assignment (storing values in all the fields at once) is an operation that will appeal to students with Pascal experience, who are accustomed to multiple assignment statements. On the other hand, they will miss the fact that there is no equivalent in Ada to Pascal's WITH.
Point out that (at least in the simple cases in this chapter) all fields of the record must be accounted for by the aggregate. Also point out the benefits of named association, which works the same as it does with subprogram parameters. Positional association is legal but more error-prone.
In discussing the Simple_Dates package, point out the ease with which a client can store a meaningless date (like Feb. 30) in the record. Prepare the students for the solution--private types, which are introduced in Chapter 11.
Note that in Ada the type of each field must have a type name. There is no equivalent to Pascal's ability to nest a record directly in another, so subrecord types must be separately declared. This leads to better decomposition in any case.
You might motivate arrays by explaining that an array could be implemented as a set of distinct scalar variables, but that it is convenient to group them together. Also note that array subscripting is indicated by parentheses as in Fortran, not by square brackets as in Pascal or C.
Emphasize that an array type declaration declares no variables and allocates no storage; it is merely a template. Also discuss the operations on arrays: assignment, equality, and aggregate initialization are all available as they are for records. This makes arrays more uniform and general than in any of the other languages your students will know. In my experience, most students don't think of using aggregate initialization, but it is very handy.
Students will find it difficult to understand why two arrays with the same structure but different types (perhaps with anonymous types) aren't compatible for equality test or assignment. Explain that the compiler isn't able to match structures, only type names. From a design perspective, if two arrays are closely related enough that one is copied to another in toto, they are closed related enough to be objects of the same type declaration. This explanation usually makes sense to them.
Spend some time on the examples in Table 9.2 and the self-check exercises at the end of the section. Work through examples that fill arrays in forward and reverse order, etc.
The case study is a nice combination of many concepts learned thus far.
Discuss the difference between sequential and random access. You might mention the analogy between arrays and external files, which can also be accessed either sequentially or randomly. Another useful analogy is a cassette tape (sequential) vs. a compact disk (random).
The discussion of how arrays are passed can be skipped if the students are all novices. Students with experience in other languages always want to know "how arrays are passed" in Ada, because in those languages the mechanism for passing is intimately associated with the mode of the parameter. This is not true in Ada. Generally, arrays are passed by reference; passing them by copy is allowed by the standard mainly to support distributed programs without shared address spaces. Some compilers will pass small arrays by copy, but most don't bother to make the distinction.
Students who know Pascal will be tempted to pass all arrays as IN OUT because they've been taught that VAR arrays are more efficient in Pascal. It makes no difference in Ada, because mode IN parameters cannot be modified, even if they are passed by reference. Passing arrays by reference will be natural to C programmers.
Partial arrays are important. Discuss array-filling thoroughly.
This is a good opportunity to discuss the benefits of having a type system that allows structures to be declared that model real-world structures naturally. Students of FORTRAN or C will be surprised that enumeration types can index arrays. Point out that floats cannot.
If this is the first time your students have seen a simple sort, go over the algorithm carefully, using a picture of the array to show the data movement. Also make sure they understand the linear search. A bit of informal discussion of "big O" will prepare them for subsequent courses.
The case study brings together nearly everything in the chapter, and shows arrays of records in a natural way. Compare the code for the sort with the procedure in Program 9.15. Point out that the algorithm is exactly the same, except for an extra field selection in the record case. In particular, show how record assignment is used to advantage. The similarity between the two sorts will prepare students for the discussion of generics in Chapter 12.