Lecture22 - ECS 30 Introduction to Programming and Problem...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the 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 (n-1); } 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(n-1); } 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(n-1); }...
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.

Page1 / 29

Lecture22 - ECS 30 Introduction to Programming and Problem...

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

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