Accumulator Recursion CS125 Spring 2007 Arthur Kantor.

Slides:



Advertisements
Similar presentations
ICE1341 Programming Languages Spring 2005 Lecture #13 Lecture #13 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Advertisements

ECE 103 Engineering Programming Chapter 54 Recursion Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed.
Recursion CS 367 – Introduction to Data Structures.
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been.
Factorial Recursion stack Binary Search Towers of Hanoi
More on Recursive Methods KFUPM- ICS Data Structures.
Lecture 3: Topics If-then-else Operator precedence While loops Static methods Recursion.
Recursion. Binary search example postponed to end of lecture.
Minnet (=stacken) public static int fac(int n) { if (n == 0) return 1; else return fac(n-1) * n; }..... main(String [] a) { int x; x = fac(3); System.out.println(x);
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
More on Recursive Recursion vs. Iteration Why Recursion?
Recursion. 2 CMPS 12B, UC Santa Cruz Solving problems by recursion How can you solve a complex problem? Devise a complex solution Break the complex problem.
CS Data Structures Appendix 1 How to transfer a simple loop- expression to a recursive function (factorial calculation)
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
Csci1300 Introduction to Programming Recursion Dan Feng.
Chapter 11 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Honors Compilers An Introduction to Algol-68S Jan 24 th 2002.
Topic R3 – Review for the Final Exam. CISC 105 – Review for the Final Exam Exam Date & Time and Exam Format The final exam is 120-minutes, closed- book,
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
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.
Inline Function. 2 Expanded in a line when it is invoked Ie compiler replace the function call with function code To make a function inline the function.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
CS0007: Introduction to Computer Programming Introduction to Arrays.
Recursion Examples Fundamentals of CS Case 1: Code /* Recursion: Case 1 */ #include void count (int index); main () { count (0); getchar(); } void count.
Stacks CISC181 Spring 2004 P. T. Conrad University of Delaware.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
Recursion. Definition Recursion is a function calling on itself over and over until reaching an end state. One such example is factorial. 10! = 10 * 9.
1 CSC103: Introduction to Computer and Programming Lecture No 11.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Chapter 8 Recursion Modified.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
1 CS161 Introduction to Computer Science Topic #10.
Dale Roberts CSCI N305 Functions Recursion Department of Computer and Information Science, School of Science, IUPUI.
CSE 425: Control Flow I Categories of Control Flow Constructs Sequencing –order of expressions and statements Selection –if, else, switch Iteration –loops.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
April 16, ICE 1341 – Programming Languages (Lecture #14) In-Young Ko Programming Languages (ICE 1341) Lecture #14 Programming Languages (ICE 1341)
1 CS161 Introduction to Computer Science Topic #17.
1 Chapter 3: Loops and Logic. 2 Control Statements If statement Example NumberCheck.java Relational operators (, >=, ==, !=) Using code blocks with If.
Tail Recursion l The case in which a function contains only a single recursive call and it is the last statement to be executed in the function. l Tail.
Lecture 15: Course Review BJ Furman ME 30 16MAY2011.
Chapter 111 Recursion Chapter Objectives become familiar with the idea of recursion learn to use recursion as a programming tool become familiar.
Recursion in Java The answer to life’s greatest mysteries are on the last slide.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
Function Recursion to understand recursion you must understand recursion.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
Chapter 8: Recursion Data Structures in Java: From Abstract Data Types to the Java Collections Framework by Simon Gray.
More on Recursive Recursion vs. Iteration Why Recursion?
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Programming Fundamentals Lecture #7 Functions
CS1061 C Prgramming Lecture 11: Functions
Control structures Chapter 3.
Recursion, Tail Recursion
CS302 - Data Structures using C++
From Recursion To Iteration: A Case Study
When a function is called...
Yan Shi CS/SE 2630 Lecture Notes
Recursion.
Learning Plan 5 Arrays.
CSCE 206 Lab Structured Programming in C
Presentation transcript:

Accumulator Recursion CS125 Spring 2007 Arthur Kantor

Last lecture: –Converting tail recursion to a while loop Today: –Converting any recursion to tail recursion

Example: fac(n) Is this in tail recursion form?

Example: fac(n) Is this in tail recursion form? No. We cannot convert it to a while loop directly.

Accumulator recursion We can convert fac(n) to accumulator recursion form. Accumulator recursion form is a kind of tail recursion form. In addition to the original parameters, it has parameters which hold the work completed so far.

Converting fac(n) to accumulator recursion form In fac(n), the work completed so far is the productSoFar So int fac(int n) becomes fac(int n, int productSoFar) The recursive case becomes

If product so far is available We can do the multiplication before the recursive call Previously we had to do the multiplication after the recursive call

Fac(n) in accumulator recursion form Is this in tail recursion form now? What must the productSoFar be in the first call to fac?

Fac(n) in accumulator recursion form Is this in tail recursion form now? Yes What must the productSoFar be in the first call to fac? fac(n,1)

Trace through two versions of fac

Converting to loop form Now we can convert fac(n, productSoFar) from acumulator recursion form to loop form

Converting to loop form Accumulator recursion form Loop form

Converting to loop form Accumulator recursion form Loop form

Converting to loop form Accumulator recursion form Loop form

Converting to loop form Accumulator recursion form Loop form

fac(n,productSoFar) must always be initially called with productSoFar =1 Convert productSoFar to a local variable outside of the loop Removing ‘extra’ parameters

Loop fac(n) vs. Recursive fac(n) ProductSoFar explicitly stores the work so far The work so far is implicitly stored in the stack of recursive calls

Another example: pow(base, exp) Is this recursive? Is this tail recursive?

Another example: pow(base, exp) Is this recursive? Yes Is this tail recursive? No What is the work so far?

Another example: pow(base, exp) Is this recursive? Yes Is this tail recursive? No What is the work so far? Again, it’s the incomplete product so far

pow(base, exp) Original –The multiplication is done after returning from the recursive call Accumulator Recursion –The multiplication is now done before the recursive call

What are the differences? Original Accumulator Recursion

What are the differences? Original Accumulator Recursion

pow(base, exp, productSoFar) Is this in tail recursive form now? What should productSoFar be in the first call to pow?

Is this in tail recursive form now? Yes What should productSoFar be in the first call to pow? 1 pow(base, exp, productSoFar)

Call trace

Preparing to convert to loop form Accumulator recursion form If-else statements switched

Converting to loop form Accumulator recursion form Loop form

Converting to loop form Accumulator recursion form Loop form

Converting to loop form Accumulator recursion form Loop form

Removing ‘extra’ arguments Loop form Final loop form