< previous page page_700 next page >

Page 700
8. General-purpose functions should be tested outside the context of a particular program, using a driver.
9. Test data should be chosen carefully to test all end conditions and some in the middle. End conditions are those that reach the limits of the structure used to store them. For example, in a one-dimensional array, there should be test data items in which the number of components is 0,1, and MAX_LENGTH (MAX_LENGTH - 1 in the case of Search2 and SearchOrd), as well as between 1 and MAX_LENGTH.
Summary
This chapter has provided practice in working with lists represented by one-dimensional arrays. We have examined algorithms that search and sort data stored in a list, and we have written functions to implement these algorithms. We can use these functions again and again in different contexts because we have written them in a general fashion.
In the searching and sorting functions, the components in the array are of type ItemType. ItemTypecan be defined as any simple data type, although slight modifications may be required to accommodate floating point types.
Strings are a special case of char arrays in C++. The last significant character must be followed by a null character to mark the end of the string. Strings are useful in working with character information. Because strings can be copied and compared using standard library functions, our generalpurpose list operations can be adapted easily to work with arrays of strings.
Quick Check
1. In a sequential search of an unordered array of 1000 values, what is the average number of loop iterations required to find a value? What is the maximum number of iterations that may be required to find a value? (pp. 667-668)
2. The following program fragment sorts a list into ascending order. Change it to sort into descending order. (pp. 653-657)
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
for (passCount = 0; passCount < length - 1; passCount++)
{
    minIndex = passCount;
    for (placeCount = passCount + 1; placeCount < length;
                                                     placeCount++)
        if (list[placeCount] < list[minIndex])
            minIndex = placeCount;
    temp = list[minIndex];                // Swap
    list[minIndex] = list[passCount];
    list[passCount] = temp;
}

 
< previous page page_700 next page >