CS367 Homework 5
Summer 2016
Due by 8 pm on Saturday, July 16

Announcements

Check here periodically.

7/11/2016  Homework assigned. To ask questions about the homework and see questions posed by other students and their answers, go to: https://piazza.com/wisc/summer2016/cs367 and sign-in using your wisc.edu account.

Questions

Homework assignments must be done individually. Collaboration on homework assignments is not allowed.

Question 1:

Assume an array arr is filled with integers. Consider the following recursive implementation to add the values in the array:

public static int add( int[] arr ) {
    //Assume arr isn't null
    return addAux(arr, 0, arr.length/2-1) + addAux(arr, arr.length/2, arr.length-1);
}
 
private static int addAux( int[] arr, int first, int last ) {
    if (first  > last) return 0;
    if (first == last) return arr[first];
    int middle = (first + last)/2;
    return addAux(arr, first, middle) + addAux(arr, middle+1, last);
}

Analyze the time complexity for the recursive addAux method above:

  1. Identify the problem size (what affects the method's runtime).
  2. Write the recurrence equations for base and recursive cases.
  3. State your solution that you've guessed by looking for a pattern.
  4. Verify your solution.
  5. Express the method's complexity in big-O notation.

Question 2:

Suppose we want to compute be for some base value b and some non-negative integer exponent e. One possible definition is as follows:

b0 = 1
be = (b × b)e / 2 if e is positive and even
be = b × (b × b)(e - 1) / 2 if e is positive and odd

Part A: Complete the power method below so that it is implemented using the recursive definition given above. You may assume that exponent is non-negative.

public static double power( double base, int exponent ) {

Part B: Analyze the time complexity for the recursive power method:

  1. Identify the problem size (what affects the method's runtime).
  2. Write the recurrence equations for base and recursive cases. For simplicity, consider only cases where exponent is a power of two.
  3. State your solution that you've guessed by looking for a pattern. For simplicity, consider only cases where exponent is a power of two.
  4. Verify your solution.
  5. Finally, express the method's (worst-case) complexity in big-O notation. For simplicity, consider only cases where exponent is a power of two.

Handing in

Please include your name at the top your file.

Put your answers to the questions into one file named Homework5 with the appropriate file extension, e.g., Homework5.pdf (see File Format for acceptable file formats).

Electronically submit your work to the Homework 5 Dropbox on Learn@UW.

Last Updated: 7/22/2014     © 2014 Beck Hasti