lecture10

lecture10 - Class 10 Recursion Introduction to Computation...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Introduction to Computation Introduction to Computation and Problem Solving and Problem Solving Prof. Steven R. Lerman Prof. Steven R. Lerman and and Dr. V. Judson Harward Dr. V. Judson Harward Class 10: Recursion Class 10: Recursion 2 Methods and Variables, 1 public class Variables { public static void main(String[] args) { int i = 42; i = incr( i ); System.out.println( "i = " + i ); i = decr( i ); System.out.println( "i = " + i ); } public static int incr( int i ) { return ++i; } public static int decr(int j) { int i = j-1; return i; } } 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
3 Methods and Variables, the Stack Are the different i variables going to interfere with each other? i=42 main i=42 main i=43 incr i=43 main i=42 main i=42 decr j=43 i=42 main 1. 2. 3. 4. 5. 4 Recursion: Can a Method Call Itself? import javax.swing.*; public class RecursiveFactorial { public static void main(String[] args) { String iString = JOptionPane.showInputDialog( "Factorial of?"); int in = Integer.parseInt( iString ); long out = rFact( in ); System.out.println( "The factorial is " + out); System.exit( 0 ); } public static long rFact ( long al ) { if ( al < 0 ) throw new IllegalArgumentException(); else if ( al == 1 || al == 0 ) return 1; else return al*rFact( al - 1 ); } } 2
Background image of page 2
5 Recursive Method on the Stack in=4 main in=4 main al=4 rFact in=4 main al=4 rFact al=3 rFact in=4 main al=4 rFact al=3 rFact al=2 rFact al=1 rFact in=4 main al=4 rFact al=3 rFact al=2 rFact al=2 rFact return 1 rFact al=3 rFact al=4 rFact in=4 main return 2 rFact al=3 rFact al=4 rFact in=4 main return 6 rFact al=4 rFact in=4 main return 24 rFact out=24 main 1. 2. 3. 4. 5. 6. 7. 8. 9. 6 Recursion A recursive algorithm takes a problem and tries to express the answer in terms of a smaller version or versions of the same type of problem. factorial( n ) = n * factorial( n – 1 ); A recursive algorithm must always have a termination condition or base case for which it can compute the answer directly. factorial( 1 ) = 1 The computer then unwinds the previous calls on the stack combining results using the recursive algorithm.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/29/2011 for the course CIVIL 1.00 taught by Professor Georgekocur during the Spring '05 term at MIT.

Page1 / 12

lecture10 - Class 10 Recursion Introduction to Computation...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online