Presentation is loading. Please wait.

Presentation is loading. Please wait.

More on Recursion Averting Program Crashes CSC 1401: Introduction to Programming with Java Week 9 – Lecture 3 Wanda M. Kunkle.

Similar presentations


Presentation on theme: "More on Recursion Averting Program Crashes CSC 1401: Introduction to Programming with Java Week 9 – Lecture 3 Wanda M. Kunkle."— Presentation transcript:

1 More on Recursion Averting Program Crashes CSC 1401: Introduction to Programming with Java Week 9 – Lecture 3 Wanda M. Kunkle

2 2 Final Example of Recursion An implementation of the Euclidean algorithm can be used to further illustrate recursion. An implementation of the Euclidean algorithm can be used to further illustrate recursion. The Euclidean algorithm dates back to the ancient Greeks and is one of the oldest algorithms known. The Euclidean algorithm dates back to the ancient Greeks and is one of the oldest algorithms known. It is an algorithm to determine the greatest common divisor (GCD) of two integers. It is an algorithm to determine the greatest common divisor (GCD) of two integers.

3 3 Final Example of Recursion The Wikipedia describes the Euclidean algorithm as follows: “Given two natural numbers a and b, not both equal to zero: check if b is zero; if yes, a is the gcd. If not, repeat the process using, respectively, b, and the remainder after dividing a by b. The remainder after dividing a by b is usually written as a mod b. ” The Wikipedia describes the Euclidean algorithm as follows: “Given two natural numbers a and b, not both equal to zero: check if b is zero; if yes, a is the gcd. If not, repeat the process using, respectively, b, and the remainder after dividing a by b. The remainder after dividing a by b is usually written as a mod b. ”Wikipedianatural numbersmodWikipedianatural numbersmod

4 4 Final Example of Recursion The algorithm can be implemented iteratively as: public static int gcd (int x, int y) { int temp; while (y != 0) { temp = y; y = x % y; x = temp; } // end while return x; } // end gcd function The algorithm can be implemented iteratively as: public static int gcd (int x, int y) { int temp; while (y != 0) { temp = y; y = x % y; x = temp; } // end while return x; } // end gcd function

5 5 Final Example of Recursion The algorithm can be implemented recursively as: public static int gcd (int x, int y) { if (y == 0) return x; else return gcd (y, x % y); } // end gcd function The algorithm can be implemented recursively as: public static int gcd (int x, int y) { if (y == 0) return x; else return gcd (y, x % y); } // end gcd function

6 6 Sample Programs Let’s look at two different sample programs that implement the Euclidean algorithm: Let’s look at two different sample programs that implement the Euclidean algorithm: –iterativeEuclid.java iterativeEuclid.java –recursiveEuclid.java recursiveEuclid.java

7 7 Recursive Function Calls A data structure called a stack can be used to keep track of recursive function calls. A data structure called a stack can be used to keep track of recursive function calls. A stack trace of the calls to function gcd required to compute the GCD of 5 and 10 appears in the next slide. A stack trace of the calls to function gcd required to compute the GCD of 5 and 10 appears in the next slide.

8 8 Recursive Function Calls Stack trace of calls to function gcd required to compute the GCD of 5 and 10: Stack trace of calls to function gcd required to compute the GCD of 5 and 10: Function call 3: X = 5, y = 0 Function call 2: X = 10, y = 5 Function call 1: X = 5, y = 10 At this point 5 is returned, and the recursion ends.

9 9 Averting Program Crashes The author of our text has provided us with a method that allows our programs to terminate “gracefully” in the event of a run-time error caused by an event such as attempted division by zero. The author of our text has provided us with a method that allows our programs to terminate “gracefully” in the event of a run-time error caused by an event such as attempted division by zero. The method, called assume, is described in the next slide. The method, called assume, is described in the next slide.

10 10 Averting Program Crashes The assume method takes one of two forms: The assume method takes one of two forms: assume (condition, error) assume (condition, error) –If the condition is true, the method allows the program to proceed normally. –If the condition is false, the method terminates the program with message error. assume (condition) assume (condition) –Works basically the same way as the above form of the method except that the standard message invalid assumption is displayed

11 11 Sample Programs Let’s look at a sample program that demonstrates how to use this method: Let’s look at a sample program that demonstrates how to use this method: –improvedOperators.java improvedOperators.java


Download ppt "More on Recursion Averting Program Crashes CSC 1401: Introduction to Programming with Java Week 9 – Lecture 3 Wanda M. Kunkle."

Similar presentations


Ads by Google