# Forint i 0 i i int submost ifsubmost

• Test Prep
• 8
• 88% (8) 7 out of 8 people found this document helpful

This preview shows page 7 - 8 out of 8 pages.

for(int i = 0; i < tree.numberOfChildren(); i++){ int submost = mostChildren(tree.child(i)); if(submost > most){ most = submost; } } } return most;
}
Complete the body of method  smallest , which computes and returns the smallest value in a  given array of  ints .  For example, if  values = {2, 0, -2, 5, 7, -1} , then   smallest  should return  -2 .  Use recursion instead of a loop.  Let the parameter  start  refer  to the first “valid” index in the array—any slots before that are not being considered in the  current call.  Assume the original call to  smallest  had a value of 0 for  start , so that they  entire array was under consideration. /** * Reports the smallest value in int[] * values. * * @requires values.length >= 1 * @ensures smallest = [the smallest value * in values] */ private static int smallest(int[] values, int start) { // Start by assuming that the first “valid” value // is the smallest in the entire array // Note that this is very similar to what you // would do if you were using a loop instead of recursion int smallestValue = values[start]; // If there is only one “valid” value in the array, // then that has to be the answer, and you’re done. // Otherwise, use recursion to find the smallest value // in the rest of the array if(start != values.length - 1){ int subsmallest = smallest(values, start + 1); // If the smallest value in the rest of the array // is smaller than the first value, then it is the // smallest value in the entire “valid” array if(subsmallest < smallestValue){ smallestValue = subsmallest; } } return smallestValue; }