Enter: A = 1 B = 7 Enter: A = 1 B = 3 Leave: A = 1 B = 3 Leave: A = 1 B = 7 2
6 2 7 1 8 0 8 0 7 1 6 2
This recursive function produces no output. The recursive calls made are: f(6,8); f(6,2); f(4,2); f(2,2); The output: 2
F entered with N = 8 F entered with N = 6 F entered with N = 4 F entered with N = 2 F entered with N = 0 F entered with N = 2 F entered with N = 4 F entered with N = 2 F entered with N = 0 F(8) = 27 N < 0 or N = 3 will result in an infinite recursion.
// Returns the sum of the first N integers in the array A. public static int ComputeSum(int A[], int N) { if(N <= 0) return 0; else // reduce the problem size return A[N - 1] + ComputeSum(A, N - 1); }
// Prints out the integers from 1 through N as a // comma separated list followed by a newline. public static void PrintIntegers(int N, int Limit) { if(N > 0) { // print out the rest of the integers then print out this integer PrintIntegers(N - 1, Limit); System.out.print(N); // test for end of string if(N != Limit) System.out.print(", "); else System.out.println("."); } // N <= 0 do nothing; }
// Outputs a line of N characters where Ch is the character. public static void WriteLine(char Ch, int N) { if(N <= 0) System.out.println(); // write rest of line else { System.out.print(Ch); WriteLine(Ch, N - 1); } }
// Outputs a block of M rows by N columns of character Ch. public static void WriteBlock(char Ch, int M, int N) { if(M > 0) { // write first line then write rest of block WriteLine(Ch, N); WriteBlock(Ch, M - 1, N); } // base case: M <= 0 do nothing. }
The return value from both calls is 1. The return value from all calls is -1. The return value from all calls is -1.
static int numEvens(LLNode<Integer> list) { if (list == null) return 0; else if ((list.getInfo() % 2) == 0) return 1 + numEvens(list.getLink()); else return numEvens(list.getLink()); }
static boolean contains(int target, LLNode<Integer> list) { if (list == null) return false; else if (list.getInfo() == target) return true; else return contains(target, list.getLink()); }
call 1: str "abcba" result false str.length() = 5 str.charAt(0) = 'a' str.charAt(4) = 'a' | ^ | | | | return true v | call 2: str "bcb" result false str.length() = 3 str.charAt(0) = 'b' str.charAt(4) = 'b' | ^ | | | | return true v | call 3: str "c" result false str.length() = 1 result = true return true
call 1: str "abcxycba" result false str.length() = 8 str.charAt(0) = 'a' str.charAt(7) = 'a' | ^ | | | | return false v | call 2: str "bcxycb" result false str.length() = 6 str.charAt(0) = 'b' str.charAt(5) = 'b' | ^ | | | | return false v | call 3: str "cxyc" result false str.length() = 4 str.charAt(0) = 'c' str.charAt(3) = 'c' | ^ | | | | return false v | call 4: str "xy" result false str.length() = 2 str.charAt(0) = 'x' str.charAt(1) = 'y' return false
int linsearch(int[] arr, int value) { return linsearch(arr,arr.length,value); int linsearch(int[] arr, int size, int value) { if (size == 0) return -1; else if (arr[size-1] == value) return size-1; else return linsearch(arr,size-1,value); }
int numeven(int[] arr) { return numeven(arr,arr.length); int numeven(int[] arr, int size) { if (size == 0) return 0; else if (arr[size-1] % 2 == 0) return 1 + numeven(arr,size-1); else return numeven(arr,size-1) }
static int linsearch(int value, int[] nums) { return linsearch(value, 0, nums); } static int linsearch(int value, int pos, int[] nums) { if (pos == nums.length) return -1; else if (nums[pos] == value) return pos; else return linsearch(value, pos+1, nums); }
static String rev(String revstr) { int end = revstr.length() - 1; if (end == 0) return revstr; else if (end == 1) return revstr; else { System.out.println(revstr); return revstr.charAt(end) + rev(revstr.substring(1,end)) + revstr.charAt(0); } }
Email Me |
Office Hours |
My Home Page |
Department Home |
MCC Home Page
© Copyright Emmi Schatz 2019