Comp 1572, Winter 2002 Pencil-and-Paper Assignment 3 Assigned Monday, January 28, 2002 Due Monday, February 4, 2002 (At the beginning of class) ============================================================================ This is pencil-and-paper homework. Write your answers as neatly as possible. If your handwriting is hard to read, please type your answers. Please begin this assignment early, so that you have time to ask questions if you have difficulty. ============================================================================ 1. An improvement can be made to the bubble sort routine. If on any pass no swaps are made, then the array is already sorted and no further passes are needed. Rewrite the bubble sort function to incorporate this improvement. Although you certainly need to test this function to make sure it works correctly, you only need to hand in the function itself. 2. Rewrite the binary search function to use a vector object rather than an array. 3. Rewrite the binary search function to be a recursive function. The function should receive an array of integers and the starting and ending subscripts as arguments. If the search key is found, return the array subscript; otherwise return -1. 4. Consider a 3-by-4 integer array t a) Write a declaration for t b) How many elements does t have? c) Write the names of all the elements in the second row of t d) Write a single statement that sets the element of t in row 1 and column 2 to 0. e) Write a nested for structure that initializes each element of t to 0 f) Write a series of statements that totals the elements of row number i g) Say what is wrong with the statement t[1,2] = 47; 5. This is problem 4.16 of D&D: Label the elements of a 3-by-5 double-subscripted array called sales to indicate the order in which they are set to zero by the following program segment: for (row = 0; row < 3; row++) for (column=0; column < 5; column++) sales[row][column] = 0; 6. This is problem 4.15 of D&D: Use a single-subscripted array to solve the following problem. Read in 20 numbers, each of which is between 10 and 100 inclusive. As each number is read, print it only if it is not a duplicate of a number already read. Provide for the "worst case" in which all 20 numbers are different. Use the smallest possible array to solve this problem. 7. Do problem 5.9 in Deitel and Deitel 8. Read problem 5.24 in Deitel and Deitel. Read it completely and carefully. It describes a recursive sort algorithm called quicksort. Do not actually do the problem, just read it and study it until you understand it 100%. This should of course include trying the algorithm with a few sample lists. If I were you, I would write the numbers on small pieces of paper so you can move them around as they are sorted. If you reach a point where you completely understand the algorithm, then write on your paper "I understand the algorithm 100%". If you can't reach that point, then write a thoughtful question, the answer to which will bring you closer to understanding it. 9. Do Self-Review Exercise 14.3, p. 789 of Deitel and Deitel. You do not need to turn this in.