|
|
|
|
|
|
|
1. In Chapter 12, the Exam program printed the names of those students taking an exam and the names of those students missing an exam. Parallel arrays were used because the struct data type had not yet been introduced. Rewrite the Exam program combining student and isPresent into a struct (type StudentType) with two members: name and isPresent. Make these structs dynamic variables rather than named variables; that is, student should be an array of pointers to structs of type StudentType. |
|
|
|
 |
|
|
|
|
As in the SortWithPointers program of this chapter, the use of dynamically allocated structs along with an array of pointers can save both execution time and memory space. |
|
|
|
|
|
|
|
|
2. In Chapter 14, the MergeLists program merged the records from three input files onto a single master file containing no duplicate records. The program uses four arrays of structs. The array sizes are fixed statically (at compile time), so memory is wasted when the input files are very small. Also, the program merges the data by copying entire structs from one array to another, which requires considerable execution time when a great amount of copying occurs. |
|
|
|
 |
|
|
|
|
Rewrite the MergeLists program so that the input records are stored into dynamically allocated structs. The four arrays should be arrays of pointers to structs. The benefits of this approach are increased memory efficiency (space is allocated only for as many structs as there are records in the files) and increased time efficiency (copying pointers during the merge operation is faster than copying entire structs). |
|
|
|
|
|
|
|
|
3. Given two arrays a and b, we can define the relation a < b to mean a[0] < b[0] and a[1] < b[1] and a[2] < b[2], and so forth. (If the two arrays are of different sizes, the relation is defined only through the size of the smaller array.) We can define the other relational operators likewise. Enhance this chapter's DynArray class by adding two Boolean member functions, LessThan and Equal. |
|
|
|
 |
|
|
|
|
These functions can be thought of as deep comparison operations because the dynamic arrays on the free store are to be compared element by element. In other words, the function call |
|
|
|
 |
|
|
|
|
arr1.LessThan(arr2) |
|
|
|
 |
|
|
|
|
returns TRUE if arr1's array elements are pairwise less than arr2's elements. |
|
|
|
 |
|
|
|
|
Test your two new functions with suitable test drivers and comprehensive sets of test data. |
|
|
|
|
|
|
|
|
4. Referring to Programming Problem 3, the client code can simulate the other four relational operators (!=, <=, >, and >=) using only the Equal and LessThan functions. However, you could make the class easier to use by supplying additional member functions NotEqual, LessOrEqual, GreaterThan, and GreaterOrEqual. Add these functions to the DynArray class in addition to Equal and LessThan. (Hint: Instead of writing each of the algorithms from scratch, simply have the function bodies invoke the existing functions Equal and LessThan. And remember: Class members can refer to each other directly without using dot notation.) |
|
|
|
 |
|
|
|
|
Test your new functions with suitable test drivers and comprehensive sets of test data. |
|
|
|
|
|
|
|
|
5. The size of a built-in array is fixed statically (at compile time). The size of a dynamic array can be specified dynamically (at run time). In both cases, however, |
|
|
|
|
|