Consider the following recursive method.
public void recursionEx(int x)
{
if(x != 0)
{
System.out.println(x);
recursionEx(x - 1);
System.out.println(x);
}
}
(a) two essential features of a recursive algorithm.
- A recursive algorithm must have a base case (terminating condition) that stops the recursion
- A recursive algorithm must have a recursive case where the algorithm calls itself with a simpler/smaller version of the problem
(b) Trace the execution of the method call recursionEx(3) and complete the table to show the sequence of calls and the values printed to the console.
(c) what would happen if the method was called by recursionEx(-3).
public static void recursionEx(int n) {
if (n < 0) {
System.out.println("Done");
} else {
System.out.println(n);
recursionEx(n - 1);
System.out.println(n);
}
}
Show the output that would be produced when the method is called with recursionEx(-3).