< previous page page_807 next page >

Page 807
// Searches list for item, returning the index if item was found.
// If item was not found, SearchOrd returns the index where
// item belongs

// Precondition:
//     length < MAX_FRIENDS
//  && list[0..length-1] are in ascending order
//  && item is assigned
// Postcondition:
//     list is the same as list@entry except that list[length] is
//       overwritten to aid in the search
//  && IF item is in list@entry
//         found == TRUE && list[index] contains item
//     ELSE
//         found == FALSE && index is where item belongs

{
    index = 0;

    // Store item at position beyond end of list

    list[length] = item;

    // Exit loop when item is found, perhaps as sentinel

    while (strcmp(item.lastName, list[index].lastName) > 0)

            // Invariant (prior to test):
            //     item is not in list[0..index-1]

        index++;

    // Determine whether item was found prior to sentinel

    found = (index < length &&
             strcmp(item.lastName, list[index].lastName) == 0);
}

//******************************************************************

void WriteEntries(
    /* in */    const EntryType addressBook[],   // Array of entries
    /* in */    int             length,          // Number of entries
    /* inout */ ofstream&       friendFile    )  // File receiving
                                                 //   list
// Writes all entries to the file friendFile

 
< previous page page_807 next page >