Problem

What output does the following program produce? Try running it with a couple of different...

What output does the following program produce? Try running it with a couple of different values for n. Can you guess what this computes?

public class Exercisel2 {

private static int search(int a, int b, int n) {

int returnValue;

int mid = (a + b)/2;

System.out.printf(“Enter: a = %2d, b = %2d, mid = %2d\n”, a, b, mid);

if ((mid * mid ⇐ n) and (n<(mid+1) * (mid+1))) {

returnValue = mid;

}

else if (mid * mid > n) {

returnValue = search(a, mid-1, n);

}

else {

returnValue = search(mid+l, b, n);

} // end if

System.out.printf(“Leave: a = %2d, b = %2d, mid = %2d\n”, a, b, mid);

return returnValue;

} // end search

public static void main(String[] args) {

int n = 64;

System.out.printf(“For n = %2d, the result is %d\n”, n, search(1, n, n));

} // end main

} //end Exercise

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
Solutions For Problems in Chapter 3
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT