Question
Some of this question has multiple answers. please keep that in mind. 1. A friend wrote a recursive routine to determine if an array contains
Some of this question has multiple answers. please keep that in mind.
1. A friend wrote a recursive routine to determine if an array contains a specific element. The attempt and its output are shown below. Explain what the problems are and fix the code. Compare your answer with the ones below. Select all correct answers.
bool isFound(int a[], int low, int high,int lookFor) { if (low==high) return false; int mid = (low+high)/2; return isFound(a,low,mid,lookFor) && isFound(a,mid+1,high,lookFor); }
void main () {int a[16] = {1,62, 15, 16,17,18,192,21,55,62,63,64,65,67,68,74} int k = 18; bool success = isFound(a,0,15,k); if (success) System.out.println(k + " Is Found"); else System.out.println( "Not Found");
k = 74; success = isFound(a,0,15,k); if (success) System.out.println(k + " Is Found"); else System.out.println( "Not Found"); }
Output is:
Not Found Not Found
The array size is specified incorrectly |
You need to change the first statement in the method to if (low==high) return a[low]==lookFor; |
You need to sort the array before it will work. |
&& needs to be || |
2. How many asterisks are printed by the following code when n=8?
void doit (int n) { if (n <=1) return; System.out.print("*"); doit(n/2); doit(n/2); }
15 |
8 |
3 |
1 |
64 |
7 |
3. How many asterisks are printed by the following code when n=8?
void doit (int n) { if (n <=1) return; System.out.print("*"); doit(n/2); }
15 |
1 |
8 |
3 |
7 |
4 |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started