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

Slides:



Advertisements
Similar presentations
Euclidean Algorithm Applied Symbolic Computation CS 567 Jeremy Johnson.
Advertisements

CS1010 Programming Methodology
Section 4.1: Primes, Factorization, and the Euclidean Algorithm Practice HW (not to hand in) From Barr Text p. 160 # 6, 7, 8, 11, 12, 13.
Copyright © 2013, 2010, and 2007, Pearson Education, Inc.
Reverse Polish Notation (RPN) & Stacks CSC 1401: Introduction to Programming with Java Week 14 – Lecture 2 Wanda M. Kunkle.
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
Algorithms. Problems, Algorithms, Programs Problem - a well defined task. –Sort a list of numbers. –Find a particular item in a list. –Find a winning.
Recursion Formatting Decimal Numbers CSC 1401: Introduction to Programming with Java Week 9 – Lectures 1 & 2 Wanda M. Kunkle.
Repetition Structures: For Loop Constants CSC 1401: Introduction to Programming with Java Week 5 Wanda M. Kunkle.
Unit 171 Algorithms and Problem Solving - II Algorithm Efficiency Primality Testing Improved Primality Testing Sieve of Eratosthenes Primality Testing.
Implementing Stacks Using Arrays CSC 1401: Introduction to Programming with Java Week 14 – Lecture 1 Wanda M. Kunkle.
Proofs, Recursion, and Analysis of Algorithms Mathematical Structures for Computer Science Chapter 2 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProofs,
Lecture 4 Discrete Mathematics Harper Langston. Algorithms Algorithm is step-by-step method for performing some action Cost of statements execution –Simple.
Unit 7 1 Unit 7: Recursion H Recursion is a fundamental programming technique that can provide an elegant solution to a certain kinds of problems H In.
Scoping and Recursion CSC 171 FALL 2001 LECTURE 8.
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
Fall 2002CMSC Discrete Structures1 Let us get into… Number Theory.
BY MISS FARAH ADIBAH ADNAN IMK
CS1101: Programming Methodology Aaron Tan.
LECTURE 5 Learning Objectives  To apply division algorithm  To apply the Euclidean algorithm.
Introduction Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
Exceptions COMPSCI 105 S Principles of Computer Science.
February 24, 2015Applied Discrete Mathematics Week 4: Number Theory 1 Modular Arithmetic Let a be an integer and m be a positive integer. We denote by.
Module :MA3036NI Cryptography and Number Theory Lecture Week 7
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 1 Introduction. Goals Why the choice of algorithms is so critical when dealing with large inputs Basic mathematical background Review of Recursion.
Recursion in C Computer Organization I 1 September 2009 © McQuain, Feng & Ribbens Recursion Around the year 1900 the illustration of the "nurse"
15-1 Chapter-18: Recursive Methods –Introduction to Recursion –Solving Problems with Recursion –Examples of Recursive Methods.
Recursive. 2 Recursive Definitions In a recursive definition, an object is defined in terms of itself. We can recursively define sequences, functions.
Copyright © Cengage Learning. All rights reserved. CHAPTER 4 ELEMENTARY NUMBER THEORY AND METHODS OF PROOF.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Data Structures Chapter 1- Introduction Mohamed Mustaq.A.
CP104 Introduction to Programming Recursion 2 Lecture 29 __ 1 Recursive Function for gcd Recursive formula for the greatest common divisor of m and n,
WIKIPEDIA HAS MANY MORE DIVISIBILITY RULES. EXAMPLE Since 52=13(4) is divisible by 4, is divisible by 4 Since 452=56(8)+4 is not divisible.
Implementing RSA Encryption in Java
Introduction to Programming (in C++) Loops Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Advanced Computer Architecture and Parallel Processing Rabie A. Ramadan http:
Application: Correctness of Algorithms Lecture 22 Section 4.5 Fri, Mar 3, 2006.
MAT 320 Spring 2008 Section 1.2.  Start with two integers for which you want to find the GCD. Apply the division algorithm, dividing the smaller number.
Application: Correctness of Algorithms Lecture 22 Section 4.5 Fri, Feb 18, 2005.
Chinese Remainder Theorem Dec 29 Picture from ………………………
CSE 311: Foundations of Computing Fall 2014 Lecture 12: Primes, GCD.
1 Programming for Engineers in Python Autumn Lecture 6: More Object Oriented Programming.
Chapter 4: Elementary Number Theory and Methods of Proof 4.8 Application: Algorithms 1 Begin at the beginning…and go on till you come to the end: then.
Euclidean Algorithm How to find a greatest common divisor in several easy steps.
1 Introduction  Algorithms  Data structures  Abstract data types  Programming with lists and sets © 2008 David A Watt, University of Glasgow Algorithms.
Application: Algorithms Lecture 20 Section 3.8 Wed, Feb 21, 2007.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
Concepts of Algorithms CSC-244 Unit 5 and 6 Recursion Shahid Iqbal Lone Computer College Qassim University K.S.A.
Application: Algorithms Lecture 19 Section 3.8 Tue, Feb 20, 2007.
CSCI 125 & 161 Lecture 12 Martin van Bommel. Prime Numbers Prime number is one whose only divisors are the number 1 and itself Therefore, number is prime.
Part II – Theory and Computations. Major Ideas....
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Dividing Decimals ( Lesson 3-4) Learn to divide decimals. ( Goal)
Number Theory Lecture 1 Text book: Discrete Mathematics and its Applications, 7 th Edition.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Program Development and Design Using C++, Third Edition
CS1010 Programming Methodology
CSCI 3333 Data Structures Recursion Exercises
Greatest Common Divisor
Greatest Common Divisor
Chapter 15 Recursion.
Chapter 15 Recursion.
Repetition and Loop Statements
While loops.
Lecture Notes – Week 4 Chapter 5 (Loops).
Application: Algorithms
Application: Algorithms
Applied Discrete Mathematics Week 10: Introduction to Counting
Recursion.
Presentation transcript:

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

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 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 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 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 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 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 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 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 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 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