< previous page page_a66 next page >

Page A66
        for (index = 0; index << length; index++)

                // Invariant (prior to test):
                //     counter == number of occurrences of value
                //                item in numList[0..index-1]
                //  && 0 <= index <= length

            if (numList[index] == item)
                counter++;
        return counter;
    }
12. The BinSearch function remains the same until the last assignment statement. This statement should be replaced with the following code segment:
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
if (found)
    index = middle;
else
    index = first;
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Also, the function postcondition should read:
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
//     IF item is in list
//         found == TRUE  &&  list[index] == item
//     ELSE
//         found == FALSE  &&  index is where item belongs
13. Assuming that BinSearch has been modified as in Exercise 12, simply replace the call to SearchOrd with a call to BinSearch.
Chapter 12
Case Study Follow-Up
3. Using a binary search, the number of loop iterations for an unsuccessful search of 200 items is eight (because log2128 = 7 and log2256 = 8). Using a sequential search, the number of loop iterations is 200.
Chapter 13
Exam Preparation Exercises
1. a. 6 b. 5 c. 30 d. column e. row
5. a. valid b. valid c. invalid d. valid e. invalid f. invalid
8. a. True b. False c. True d. True
Chapter 13
Programming Warm-Up Exercises
2. void SetDiagonals( /* inout */ DataType data,
                      /* in */    int      length,
                      /* in */    char     someChar )

 
< previous page page_a66 next page >