Recursive program Zhen Jiang West Chester University

Slides:



Advertisements
Similar presentations
Spring Semester 2013 Lecture 5
Advertisements

Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
Recursion. Recursive Definitions A recursive definition is one which uses the word being defined in the definition Not always useful:  for example, in.
Lecture 3: Topics If-then-else Operator precedence While loops Static methods Recursion.
Introduction to Computers and Programming Introduction to Methods in Java.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Encapsulation by Subprograms and Type Definitions
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Copyright © 2003 Pearson Education, Inc. Slide 1.
1 MATERI PENDUKUNG METHOD Matakuliah: M0074/PROGRAMMING II Tahun: 2005 Versi: 1/0.
Introduction to Methods
Python quick start guide
Imperative Programming
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 9/17/20151.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
Runtime Environments Compiler Construction Chapter 7.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Recursion Chapter 11. The Basics of Recursion: Outline Introduction to Recursion How Recursion Works Recursion versus Iteration Recursive Methods That.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Chapter 5: Programming Languages and Constructs by Ravi Sethi Activation Records Dolores Zage.
CIS 068 Welcome to CIS 068 ! Stacks and Recursion.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
ECE122 Feb. 22, Any question on Vehicle sample code?
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Recursion Using Stacks. What’s Going On? int main() { // Get Input int x, y; cout > x; // Call f y = f(x); // Print results.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Recursion.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Recursion Continued Divide and Conquer Dynamic Programming.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
Recursion ITFN The Stack. A data structure maintained by each program at runtime. Push Pop.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Function – I What is a function Why we need functions?
Run-Time Environments Chapter 7
User-Written Functions
Data Type and Function Prepared for CSB210 Pemrograman Berorientasi Objek By Indriani Noor Hapsari, ST, MT Source: study.
5.13 Recursion Recursive functions Functions that call themselves
Principles of programming languages 4: Parameter passing, Scope rules
JavaScript: Functions
©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Functions.
CSC141 Computer Science I Zhen Jiang Dept. of Computer Science
CSC115 Introduction to Computer Programming
Chapter 5 - Functions Outline 5.1 Introduction
Zhen Jiang West Chester University
4-2 Functions in C In C, the idea of top–down design is done using functions. A C program is made of one or more functions, one and only one of which.
CSC115 Introduction to Computer Programming
Chapter 4 Functions Objectives
Stack Memory 2 (also called Call Stack)
CSC141 Computer Science I Zhen Jiang Dept. of Computer Science
Zhen Jiang West Chester University
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
UNIT V Run Time Environments.
4-2 Functions in C In C, the idea of top–down design is done using functions. A C program is made of one or more functions, one and only one of which.
Zhen Jiang West Chester University
Functions and Recursion
Recursion.
Topic 2b ISA Support for High-Level Languages
Introduction to Methods and Interfaces
Presentation transcript:

Recursive program Zhen Jiang West Chester University

Outline Review of function/method Introduction Design idea Keys Static Analysis Examples

Review of Function/Method Why function/method To avoid copying the same code in many places Format Call Declaration Working procedure Details of the use of function/method Functions without arguments ([2], p120) Functions with arguments ([2], p130) Value and reference parameters ([2], p296) Using object with function, & ([2], p321) Return Using pointer with function (will be reviewed later)

Introduction Divide-and-conquer multi-layer job division (job becomes smaller) Results collection Result reporting (to caller)

Introduction (cont.) f ( Job ) f ( Job.sub-job1 )f ( Job.sub-job2 ) f ( Job.sub-job1. sub-sub-job1 ) f ( Job.sub-job1. sub-sub-job2 )

Keys Division (The assignment and reporting of the requested job is divided into smaller pieces: job division, results collection, and result reporting, in divide-conquer tree by many non-leaf node.) Recursive procedure (ensure each node in the divide-conquer tree has similar procedure that can be written in a general/common format, but more abstract) Terminated condition (decide who will really do the job) Samples to see these keys (in static analysis) Introduction (cont.)

Static Analysis Check the whole execution. Think about: You are a computer and run the program statement by statement. Need to check the result after each statement. Show the procedure called at different time.

Examples f(int i) { if (i ==0) return 1; else return i*f(i-1); } 4*f(3)3*f(2)2*f(1)1*f(0) f(4)? job divisionResults collection Result reporting Terminated condition General format, all same f(4)f(3)f(2)f(1)f(0)

Examples (cont.) f(int i) { if (i <=1) return 1; else return f(i-1)+f(i-2); } f(3)? f(2)+f(1)f(1)+f(0) f(3)f(2)f(1)f(0)

Examples (cont.) f(int i) { int y=i; Static int z=0; z++; if (i <=1) { System.out.println(y+” ”+z); return 1; } else return f(i-1)+f(i-2); } f(3)? f(4)? f(2)+f(1) f(1)+f(0) f(3)f(2)f(1)f(0) y=3 z=0 z=1 y=2 z=2 y=1 z=3 1 3 y=0 z=4 0 4 ? 1 f(1) y=1 z=5 1 5

Examples (cont.) f(int i) { if (i <=1) return 1; else if (i==3) return f(i-1)-f(i-2); else return f(i-1)+f(i-2); } f(3)? f(2)-f(1)f(1)+f(0) f(3)f(2)f(1)f(0)