Introduction to Programming (22C:16, 22C:106)

Homework 10: Due Thursday, 5/8/97

Problem 1: [7 points]

Write a recursive function called maximum that takes in as parameters a Vector of integers called list and a positive integer called N and returns the maximum integer among

 list[0], list[1],...,list[N-1].

Use the following function header:

int maximum (const Vector<int> & list, int N)

You may assume that N is less than or equal to list.length().

Problem 2: [6 points]

Rewrite the binary search function provided in class as a recursive function. Use the following function header:

int binarySearch (const Vector<int> & list, int key, int first, int last)

You may assume that when the above function is called, 0 is passed in for first and list.length()-1 is passed in for last. Further assume that this function simply returns 1 if key is found and returns 0 otherwise.

Problem 3: [7 points]

As you know, every integer has a binary representation, that is, a representation only in terms of 0's and 1's. For example, the binary representation of 5 is 101, the binary representation of 12 is 1100, and so on. Below, I describe an algorithm to convert a given positive integer into its binary representation.

To find the binary representation of a given integer N, take the binary representation of N/2 and to that append a 1, if N is odd or append a 0, if N is even. The binary representation of the integer 1 is 1 itself.

Translate this algorithm into a recursive function that takes in a positive integer N and returns a string of 0's and 1's that is the binary representation of N. Use the following function header:

string toBinary (int N)


Sriram Pemmaraju
Thu May 1 21:43:29 CDT 1997