f
below:
int f(int a, int b) { int ans; if (a > b) ans = 0; else ans = a + f(a+1, b); return ans; }
f(4,6)
. Show the parameters and return
values at each step.
f(a,b)
return? That is,
what task does f
perform?
target
in array a
, or -1
if target
does not appear in a
.
int find(int[] a, int target, int index) { int loc; if (a[index] == target) loc = index; else loc = find(a, target, index+1); return loc; }
Fill in the code for power below to make it a recursive method to do the power computation.
int power(int base, int exp) { int pow; // if exp is 0, set pow to 1 // otherwise make recursive call and store result in variable pow // return pow }