This preview shows pages 1–8. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: ECS 30 Introduction to Programming and Problem Solving Franois Gygi Department of Computer Science Lecture 22 Chapter 10 Recursion Homework 6 due date moved to Sat Nov 19, 11:50pm Recursion Some tasks can be decomposed into subtasks In some cases, a subtask looks just like the original task Example: compute the factorial of 5 5! = 5 * 4 * 3 * 2 * 1 5! = 5 * 4! A function that performs the subtask can be used to perform the original task Recursion The function implementing the factorial calculation is calling itself int fac (int n) { if ( n == 1 ) return 1; else return n * fac (n1); } Recursion Compare with the iterative implementation int fac(int n) { int i, product = 1; for ( i=2; i<=n; i++ ) product *= i; return product; } Defining a recursive function Identify the special case where the evaluation of the function is "simple" int fac(int n) { if ( n == 1 ) return 1; else return n * fac(n1); } Defining a recursive function Deal with the other cases, possibly including a recursive call int fac(int n) { if ( n == 1 ) return 1; else return n * fac(n1); }...
View
Full
Document
This note was uploaded on 01/21/2012 for the course ECON 2a taught by Professor Xi during the Spring '11 term at Cornell College.
 Spring '11
 xi
 Macroeconomics

Click to edit the document details