Scoping and Recursion CSC 171 FALL 2004 LECTURE 12.

Slides:



Advertisements
Similar presentations
Introduction to Recursion and Recursive Algorithms
Advertisements

Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 12: Recursion.
Computer Science II Recursion Professor: Evan Korth New York University.
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
Proof Techniques and Recursion. Proof Techniques Proof by induction –Step 1: Prove the base case –Step 2: Inductive hypothesis, assume theorem is true.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
16-Jun-15 javadoc. 2 Javadoc placement javadoc comments begin with /** and end with */ In a javadoc comment, a * at the beginning of the line is not part.
Unit 191 Recursion General Algorithm for Recursion When to use and not use Recursion Recursion Removal Examples Comparison of the Iterative and Recursive.
CSSE221: Software Dev. Honors Day 16 Announcements Announcements Markov due Wednesday 11:59 pm Markov due Wednesday 11:59 pm Homework 6 due next Tuesday,
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
Recursion Road Map Introduction to Recursion Recursion Example #1: World’s Simplest Recursion Program Visualizing Recursion –Using Stacks Recursion Example.
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 14 Recursion.
1 Recursion Recursive Thinking Recursive Programming Recursion versus Iteration Direct versus Indirect Recursion More on Project 2 Reading L&C 10.1 – 10.4.
Recursion!. Can a method call another method? YES.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
More on Recursion Averting Program Crashes CSC 1401: Introduction to Programming with Java Week 9 – Lecture 3 Wanda M. Kunkle.
Scoping and Recursion CSC 171 FALL 2001 LECTURE 8.
An Introduction to Fractals By: Brian Feuer What is a Fractal? A word coined by Benoit Mandelbrot in 1975 to describe shapes that are “self-similar”
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 19 Recursion.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea.
Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1.
CS-2852 Data Structures LECTURE 12B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Recursion and Dynamic Programming CSC 172 SPRING 2004 LECTURE 9.
Recursive. 2 Recursive Definitions In a recursive definition, an object is defined in terms of itself. We can recursively define sequences, functions.
Chapter 7 Recursion Recursive methods Recursion in two- dimensional grids Recursive helper method Analysis of recursive algorithms.
1 Joe Meehean.  call themselves directly  or indirectly void f(){... f();... } void g(){... h();... } void h(){... g();... }
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
What does a computer program look like: a general overview.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Recursion. Math Review Given the following sequence: a 1 = 1 a n = 2*a n-1 OR a n+1 = 2*a n What are the values of the following? a 2 = a 3 = a 4 =
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
1 Recursion n what is it? n how to build recursive algorithms n recursion analysis n tracing simple recursive functions n hands on attempts at writing.
1 Lecture 3 Part 2 Storage Classes, Scope, and Recursion.
RECURSION Go back, Jack, and do it again.. Recursion 2  Recursion occurs whenever "something" is defined in terms of itself.  Structural recursion:
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.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Maitrayee Mukerji. Factorial For any positive integer n, its factorial is n! is: n! = 1 * 2 * 3 * 4* ….* (n-1) * n 0! = 1 1 ! = 1 2! = 1 * 2 = 2 5! =
Program Development and Design Using C++, Third Edition
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
Recursion You may have seen mathematical functions defined using recursion Factorial(x) = 1 if x
Recursion.
CS212: Data Structures and Algorithms
Recursion The programs discussed so far have been structured as functions that invoke one another in a disciplined manner For some problems it is useful.
Introduction to C++ Programming Language
Data Structures with Java © Rick Mercer
Chapter 15 Recursion.
C Functions -Continue…-.
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
Chapter 15 Recursion.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Unit 3 Test: Friday.
Recursive GCD Demo public class Euclid {
RECURSION Annie Calpe
class PrintOnetoTen { public static void main(String args[]) {
Recursion Taken from notes by Dr. Neil Moore
Java Programming: Chapter 9: Recursion Second Edition
Recursion.
Recursion Method calling itself (circular definition)
CMPT 120 Lecture 18 – Unit 3 – Graphics and Animation
Presentation transcript:

Scoping and Recursion CSC 171 FALL 2004 LECTURE 12

JAVADOC Setting Documentation comments in source – Delimited by “/**” and “*/” You can comment – Classes – Methods – Fields Each comment is placed immediately above the feature it documents

@version

Using Javadoc Demo

Parameter Scoping public class myClass { int x = 4; public static void main(String args[]){ int x = myMethod1(5); System.out.println(“x ==“ + x); } public static int myMethod1(int x) { myMethod2(x); return x + 1; } public static int myMethod2(int x) { x = 7; } }

Parameter Scoping public class myClass { int y = 0 ; public static void main(String args[]){ int x = myMethod1(5); y+=x; System.out.println(“x ==“ + x + “ y == “ + y); } public static int myMethod1(int x) { y+= x; return myMethod2(x) + 1; } public static int myMethod2(int x) {y+=x; return x + 1; } }

Recursion Methods can call other methods Methods can also call themselves Consider the “factorial” method – 4! = 4*3*2*1 – We can think of this as 4! = 4*3! – 3! = 3*2! – 2! = 2* 1! – 1! = 1 Recursion is when we think of things in terms of itself.

Recursion We often think of recursion as a “programming technique” Recursion is also a design pattern for data structures Recursion is a way to think about problem solving

A Theological Aside Disclaimer: you don’t have to believe Can computer science inform theology? Christian notion of “Trinity”? "A three-fold personality existing in one divine being or substance; the union in one God of Father, Son, and Holy Spirit as three infinite, co-equal, co- eternal persons; one God in three persons." from about 400 AD to about 1800 AD, lots of people were put to death for refusing to believe in the idea of "one God in three persons."

To this day “Trinity is still largely incomprehensible to the mind of man.” – J. Hampton Keathley, III, Th.M But is it? How can something be “3 in 1”? RECURSION!

Sierpinski Triangle This design is called Sierpinski's Triangle, after the Polish mathematician Waclaw Sierpinski who described it in Among these is its fractal or self-similar character. The large blue triangle consists of three smaller blue triangles, each of which itself consists of three smaller blue triangles, each of which..., a process of subdivision which could, with adequate screen resolution, be seen to continue indefinitely.

Serpinski Triangle What is a Serpinski Triangle? A Serpinski Triangle is a triangle made up of 3 Serpinski triangles.

Weiss’s fundamental rules of recursion 1. Base case: always have at least one case that can be solved without recursion 2. Make progress: Any recursive call must progress toward the base case 3. “You gotta believe”: Always assume that the recursive call works. (In proofs “BTIH”). 4. Compound Interest rule: Never duplicate work by solving the same instance of a problem in separate recursive calls.

Example : Fibonacci Number public static long fib (int n) { if (n <= 1) return n; else return fib (n-1) + fib(n-2); }

Recursion Methods can call other methods Methods can also call themselves public long factorial(long number) { if (number <=1) return 1; else return number * factorial(number –1); }

Recursion vs. Iteration GCD – Euclid’s (300 B.C.) Algorithm – Oldest known non-trivial algorithm – If r is the remainder when a is divided by b – Then the common divisors of a and b are the same as the common divisors of b and r

Examples of Euclid’s Alg GCD(206,40) ==GCD(40,6) ==GCD(6,4) ==GDC(4,2) ==GCD(2,0) ==2 GCD(154,35) ==GCD(35,14) ==GCD(14,7) ==GCD(7,0) ==7

In Class Excercise Using Euclids algorithm write: public int GCD(int a, int b) Partner up 10 min hand in on paper – two names

Iterative version public static int GCD1(int a, int b) { while (b != 0){ int temp = a%b; a = b; b = temp; } return a; }

Recursive version public static int GCD2(int a, int b) { if (b == 0 ) return a; else return GCD2(b,a%b); }