Problem Set Answers: Recursion


  1. Enter: A = 1 B = 7
    Enter: A = 1 B = 3
    Leave: A = 1 B = 3
    Leave: A = 1 B = 7
    2
    
  2. 6  2
    7  1
    8  0
    8  0
    7  1
    6  2
    
  3. 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
    
  4. 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.
    
  5. // 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);
    }
    
  6. // 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;
    }
    
  7. // 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);
       }
    }
    
  8. // 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.
    }
    
  9.  

    The return value from both calls is 1.

    The return value from all calls is -1.

    The return value from all calls is -1.
    1. For a binary search to work the array must first be sorted
    2. Index = (0 + 101)/2 = 50
    3. Number of comparisons = 7
  10.    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());
       }
    
  11.    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());
       }
    
    1.    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
      
    2.    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
      
  12. Mercury
    Saturn
    Jupiter
    Mars
  13.    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);
       }
    
  14.    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)
        }
    
  15.     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);
    	}
    
  16. 	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