Computer Science 2611
Summer 2000
Laboratory Assignment 12 (last one)

Handed out: 7/21/00

Note. Before doing this assignment you will need a working solution to assignment 10.

Your solution to assignment 10 defines a constant such as MAX_NAME_LENGTH for the number of characters that a name can occupy, and another such as MAX_NUMBER_LENGTH for the maximum number of characters in a number. When a string is stored, the maximum number of bytes is used, regardless of how long the string is. Often, most of the space is wasted.

For example, suppose that MAX_NAME_LENGTH is chosen to be 100, to allow for long names. To store the name "Bob", your program uses 101 bytes. Only the first four of those bytes will have useful information in them; the remaining 97 bytes are wasted.

It is possible to reduce the waste, using only as many bytes as are needed for each name. Instead of the names array being a two-dimensional array, it can be an array of pointers. So names can be created by writing

  char* names[MAX_ENTRIES];
where MAX_ENTRIES is the maximum number of entries in the phone book.

When you read a name, set aside an array to read it into. Allocate MAX_NAME_LENGTH+1 bytes for that array. (This array can be created in the usual way; you do not need to use new for it.) Do a similar thing for the number. After reading the name and number into those arrays, allocate new arrays (using the new operator) that are just large enough to hold the name and number. For example, if the name has been read into array nameArray, then say

    char* namep = new char[strlen(nameArray) + 1];
to allocate the memory, and then use strcpy to copy the string from array nameArray to the array pointed to by namep.
    strcpy(namep, nameArray);
Having copied the name into the array pointed to by namep, array nameArray can be reused to read the next name.

Test your program and after it is working turn it in in the usual way.