{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture6RecursionReiterationE7

lecture6RecursionReiterationE7 - E7 Spring 2010 Lecture 6...

This preview shows pages 1–10. Sign up to view the full content.

E7 Spring 2010: Lecture 6 Raja Sengupta College of Engineering University of California, Berkeley

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

View Full Document
Topics Practice tracing recursive programs • Iteration – Textbook section 4.5 More about recursive definitions – Pitfalls of recursive definition – Inductive definitions are safe – More recursion problems
Tracing Fibonacci function out = fib(n) if gt(n,2) out = fib(n-1) + fib(n-2); elseif or(eq(n,1),eq(n,2)) out = 1; end end fib(5) n = 5 out = fib(4) + fib(3) fib(4) n = 4 out = fib(3) + fib(2) fib(3) n = 3 out = fib(2) + fib(1) fib(3) n = 3 out = fib(2) + fib(1) fib(2) n = 2 out = 1 fib(2) n = 2 out = 1 fib(1) n = 1 out = 1 fib(2) n = 2 out = 1 fib(1) n = 1 out = 1

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

View Full Document
Tracing Fibonacci fib(5) n = 5 out = fib(4) + fib(3) fib(4) n = 4 out = fib(3) + fib(2) fib(3) n = 3 out = fib(2) + fib(1) fib(3) n = 3 out = fib(2) + fib(1) fib(2) n = 2 out = 1 fib(2) n = 2 out = 1 fib(1) n = 1 out = 1 fib(2) n = 2 out = 1 fib(1) n = 1 out = 1 1 1 1 1 1 1 1 1 2 1 1 2 3 2 2 3 5 function out = fib(n) if gt(n,2) out = fib(n-1) + fib(n-2); elseif or(eq(n,1),eq(n,2)) out = 1; end end
Tracing Fibonacci fib(5) n = 5 out = fib(4) + fib(3) fib(4) n = 4 out = fib(3) + fib(2) fib(3) n = 3 out = fib(2) + fib(1) fib(3) n = 3 out = fib(2) + fib(1) fib(2) n = 2 out = 1 out = 1 fib(1) n = 1 out = 1 fib(2) n = 2 out = 1 fib(1) n = 1 out = 1 1 1 1 1 1 1 1 2 1 1 2 3 2 2 3 5 At each function call I keep track of all the variables. function out = fib(n) if gt(n,2) out = fib(n-1) + fib(n-2); elseif or(eq(n,1),eq(n,2)) out = 1; end end

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

View Full Document
function out = factorialIter(n) if gt(n,0) out = 1; for counter = 1:1:n out = counter*out; end else out = 1; end end Iteration: The other way of making a computer compute the same thing many times help for FOR variable = expr, statement, ..., statement END This is the for-loop
function out = factorialIter(n) if gt(n,0) out = 1; for counter = 1:1:n out = counter*out; end elseif eq(n,0) out = 1; end end help for FOR variable = expr, statement, ..., statement END For every value of counter starting at 1 and ending at n in increments of 1, evaluate the statements between for and end Iteration: The other way of making a computer compute the same thing many times

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

View Full Document
Tracing Iteration help for FOR variable = expr, statement, ..., statement END factorialIter(4) out = 1 counter = 1 out = 1 counter = 2 out = 2 counter = 3 out = 6 counter = 4 out = 24 function out = factorialIter(n) if gt(n,0) out = 1; for counter = 1:1:n out = counter*out; end else out = 1; end end
Tracing Iteration help for FOR variable = expr, statement, ..., statement END factorialIter(4) = 24 out = 1

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 32

lecture6RecursionReiterationE7 - E7 Spring 2010 Lecture 6...

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

View Full Document
Ask a homework question - tutors are online