Suggested Assignments

1. a. Implement the collection data abstraction of Chapter 1 using an array to store the actual integers in the collection. That is, implement the operations set, insert, omit, and belongs.

b. Implement a collection c of carrecords as a data abstraction with the operations set (c), insert (p,c), omit (i,c), and printrecord (i,c). Use the carrecords defined in Section 2.1.3 but add an additional field for an identification number for each car. Then, insert (p,c) adds the carrecord pointed to by p to the collection c, omit (i,c) deletes the carrecord with the identifier i from c, and then printrecord (i,c) prints the contents of the carrecord with identifier i.

c. This is the same as (b), except the records are to be stored in dynamic memory with a pointer array used to contain pointers to them.

2. Suppose the amount of precipitation has been recorded over a region at n locations. In general each record consists of one to five numbers. You are to write and run six programs. Each is to produce the average precipitation. For example, if the input is

3.6

2.1

2.7

record sentinel

.67

1.2

record sentinel

1.9

record sentinel

final sentinel

the output should be the sum [(3.6 + 2.1 + 2.7) + (.67 + 1.2) + 1.9] divided by 6. Always echo print the input (that is, output the input as it is read in), and annotate all output so that you, or anyone, will know what the output means. Assume n ?/FONT> 100.

a. Two of the programs should assume all records are of the same length (say, 3). Program 1 will represent the records as fixed-length homogeneous records stored in a single array (method 1 of Section 2.4). Program 3 will represent them as fixed-length records stored in multiarrays (method 3 of Section 2.4). For method 3, assume an additional record field giving the specific location where it was recorded.

b. Three of the programs should assume the general case of C structures that contain a union and store them in an array (method 2 of Section 2.4). Program 4 will use a relative pointer array whose entries point to records stored in an array (method 4 of Section 2.4). The fifth program uses pointer variable arrays whose entries point to records stored in dynamic memory. For methods 4 and 5, output the contents of the pointer arrays and the records to which each pointer points. Your programs 4 and 5 should work correctly no matter where the records are stored in the one-dimensional array.

c. Finally, one program should assume all records are of length 1 and represent entries in an n ?/FONT> n symmetric array. This program uses the symmetric array representation of Example 2.6. No two-dimensional array should be used in your solution.

The input and output for each program might appear as follows:

                Input                                Output

------------------------------------------------------------------------------

Programs A  1.    3.6            Echo print of the precipitation records:

                  2.1                3.6, 2.1, 2.7

                  2.7                .67, 1.2, 1.9

                  RS             The average precipitation of the 6 values is

                 .67             2.028 inches. The number of records is 2.

                  1.2

                  1.9

                  RS

                  FS

            3. White House       Echo print of the precipitation records:

                  3.6                White House 3.6, 2.1, 2.7

                  2.1                Lincoln Memorial .67, 1.2 , 1.9

                  2.7            The average precipitation of the 6 values is

                  RS             2.028 inches. The number of records is 2.

               Lincoln Memorial

                  .67

                  1.2

                  1.9

                  RS

                  FS

                Input                                 Output

-----------------------------------------------------------------------------

Programs B  2.    3.6             Echo print of the precipitation records:

                  2.1                3.6, 2.1, 2.7

                  2.7                .67, 1.2

                  RS                 1.9

                  1.2

                  RS              The average precipitation of the 6 values is

                  1.9             2.028 inches. The number of records is 3.

                  RS

                  FS

            4.    3.6             Echo print of the precipitation records:

                  2.1                3.6, 2.1, 2.7

                  2.7                .67, 1.2

                  RS                 1.9

                 .67              Pointer array  Data array

                  1.2                1    10     10    3.6

                  RS                 2     1     11    2.1

                  1.9                3    16     12    2.7

                  RS

                  FS                              1     .67

                                                  2    1.2

                                                 16    1.9  

                                  -------------------------

                                  The average precipitation of the 6 values is

                                  2.028 inches. The number of records is 3.

            5. This is the same   Records in dynamic storage

               as for 4, except      3.6      2.1   2.7

               it will print the      .67     1.2

               pointer array         1.9

               (that is, the 

               actual pointers).

Programs C     3.6   2.1  2.7     Echo print of the precipitation records as a

                .67  1.2  1.9     3 ?/FONT> 3 array:

               0.0   1.1   .15        3.6     2.1   2.7

                                       .67    1.2   1.9

                                      0.0     1.1    .15

                                  The average precipitation of the 9 values is 

                                  2.028 inches.

3. Modify the program of Section 2.5.2 so that its input consists of the men's and women's preferences and then a series of entire pairings. For each of the pairings, it is to call stabilitycheck and print the result for the pairing.