CS367 Homework 5
| |||||
AnnouncementsCheck here periodically.
| |||||
QuestionsHomework 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:
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:
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:
| |||||
Handing inPlease 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 |