|
|
|
|
|
|
|
By assuming that all students have unique last names, we have simplified the problem considerably. Programming Problem 2 is similar, but you must account for both first and last names. |
|
|
|
|
|
|
|
|
In this chapter, we have discussed and coded six general-purpose functions: three sequential searches, a binary search, a selection sort, and an insertion into an ordered list (which can also be used as a sort). We have used three of these functions in the Exam program that has been tested. We test the other three functions by embedding them in driver programs. The drivers should read in data, call the function, and print out the results. Following is the algorithm for a driver to test the Search2 function: |
|
|
|
|
|
|
|
|
Get list of components
WHILE more items to be searched for
Get item
Search(list, item, length, index, found)
IF found
Print item, "found at position", index
ELSE
Print item, "not found in list" |
|
|
|
|
|
|
|
|
The driver would have to be run with several sets of test data to test the Search2 function thoroughly. The minimum set of lists of components would be: |
|
|
|
|
|
|
|
|
1. A list of no components |
|
|
|
|
|
|
|
|
2. A list of one component |
|
|
|
|
|
|
|
|
3. A list of MAX_LENGTH - 1 components |
|
|
|
|
|
|
|
|
4. A list of more than one but less than MAX_LENGTH - 1 components |
|
|
|
|
|
|
|
|
The minimum set of items being searched for would be: |
|
|
|
|
|
|
|
|
1. item in list[0]
2. item in list[ length-1 ]
3. item between list[0] and list[length-1]
4. item< list[0]
5. item > list[length-1]
6. item value between list[0] and list[length-1] but not there |
|
|
|
|
|
|
|
|
Because ItemType can be any data type that the relational operators can be applied to, function Search2 should be tested with components of several different types. |
|
|
|
|
|