Ch. 18 Recursion

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

One or more base cases (the simplest case) are used to stop recursion. Every recursive call reduces the original problem, bringing it increasingly close to a base case until it becomes that case.

Describe the characteristics of recursive methods.

six times. (base case factorial(0))

How many times is the factorial method in Listing 18.1 invoked for factorial(6)?

(a) Sum is 15 (5 + 4 + 3 + 2 + 1 = 15) (b) 7654321

Show the output of the following programs and identify base cases and recursive calls. (a) public class Test { public static void main(String[] args) { System.out.println( "Sum is " + xMethod(5)); } public static int xMethod(int n) { if (n == 1) return 1; else return n + xMethod(n - 1); } } (b) public class Test { public static void main(String[] args) { xMethod(1234567); } public static void xMethod(int n) { if (n > 0) { System.out.print(n % 10); xMethod(n / 10); } } }

(a) The output is 5 4 3 2 1 (b) The output is 1 2 3 4 5

Show the output of the following two programs: (a) public class Test { public static void main(String[] args) { xMethod(5); } public static void xMethod(int n) { if (n > 0) { System.out.print(n + " "); xMethod(n - 1); } } } (b) public class Test { public static void main(String[] args) { xMethod(5); } public static void xMethod(int n) { if (n > 0) { xMethod(n - 1); System.out.print(n + " "); } } }

False

T/F: A recursive method is invoked differently from a non-recursive method.

True

T/F: Every recursive call reduces the original problem, bringing it increasingly closer to a base case until it becomes that case.

True

T/F: Every recursive method must have a base case or a stopping condition.

False

T/F: Every recursive method must have a return value.

Tru

T/F: Infinite recursion can occur if recursion does not reduce the problem in a manner that allows it to eventually converge into the base case.

A recursive method is the one that calls itself. An infinite recursion is the one that never stops.

What is a recursive method? What is an infinite recursion?

(a) n is double. There is no guarantee that n != 0 will be eventually false.

What is wrong in the following method? (a) public class Test { public static void main(String[] args) { xMethod(1234567); } public static void xMethod(double n) { if (n != 0) { System.out.print(n); xMethod(n / 10); } } }

(b) Infinite recursion due to new Test() inside the constructor Test().

What is wrong in the following method? (b) public class Test { public static void main(String[] args) { Test test = new Test(); System.out.println(test.toString()); } public Test() { Test test = new Test(); } }

f(n) = 1 if n = 1 f(n) = f(n-1) + n for (n > 1)

Write a recursive mathematical definition for computing 1 + 2 + 3 + ... + n for a positive integer n.

f(n) = 2 if n = 1 f(n) = 2 * 2^(n-1) for (n > 1)

Write a recursive mathematical definition for computing 2^n for a positive integer n.

f(n) = x if n = 1 f(n) = x * x^(n-1) for (n > 1)

Write a recursive mathematical definition for computing x^n for a positive integer n and a real number x.


Ensembles d'études connexes

PP RNSG 1137 Communications Mastery Quiz

View Set

Chapter 8: Foreign Direct Investment

View Set

Euro History - "The Industrial Revolution"

View Set