Zhen Jiang West Chester University

Slides:



Advertisements
Similar presentations
Recursive program Zhen Jiang West Chester University
Advertisements

Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
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.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2007 Pearson Education, Inc. All rights reserved C Functions.
1 MATERI PENDUKUNG METHOD Matakuliah: M0074/PROGRAMMING II Tahun: 2005 Versi: 1/0.
Introduction to Methods
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
METHODS Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
© 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 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.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
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.
CPS120: Introduction to Computer Science Functions.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 12/11/20151.
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
Chapter 5 Methods. 2 Contents 1. Introduction to Methods 2. Passing Arguments to a Method 3. More about Local Variables 4. Returning a Value from a Method.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Recursion Powerful Tool
Function – I What is a function Why we need functions?
Run-Time Environments Chapter 7
Functions.
Data Type and Function Prepared for CSB210 Pemrograman Berorientasi Objek By Indriani Noor Hapsari, ST, MT Source: study.
Chapter 7 User-Defined Methods.
Programming with ANSI C ++
Methods Chapter 6.
JavaScript: Functions
CS1061 C Prgramming Lecture 11: Functions
©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Functions.
Lecture Set 4 Data Types and Variables
CSC141 Computer Science I Zhen Jiang Dept. of Computer Science
11/10/2018.
CSC 253 Lecture 8.
Chapter 5 - Functions Outline 5.1 Introduction
Chapter Topics Chapter 5 discusses the following main topics:
Starting Out with Java: From Control Structures through Objects
Starting Out with Programming Logic & Design
Lecture 11 C Parameters Richard Gesick.
CSC 253 Lecture 8.
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
Chapter 6 Methods: A Deeper Look
CSC141 Computer Science I Zhen Jiang Dept. of Computer Science
Defining Classes and Methods
Recursion Chapter 11.
Chapter 6 – Methods Topics are:
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.
Week 4 Lecture-2 Chapter 6 (Methods).
Methods.
Starting Out with Programming Logic & Design
Instance Method – CSC142 Computer Science II
Zhen Jiang West Chester University
Java Methods: A Deeper Look Academic 2019 Class: BIT23/BCS10 Chapter 06 Abdulaziz Yasin Nageye Faculty of Computing Java How to Program, 10/e 1 © Co py.
Parameters and Arguments
Presentation transcript:

Zhen Jiang West Chester University zjiang@wcupa.edu Recursive program Zhen Jiang West Chester University zjiang@wcupa.edu

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

Review of Function/Method Why function/method To avoid copying the same code in many places Format (Figure 5-3, page 256) Call Declaration (public static, page 551) Working procedure (Figure 5-4, page 258) Details of the use of function/method Argument and parameter (Figure 5-7, page 264) Type Compatibility and casting (page 266) Multiple arguments passed into multiple parameters (Figure 5-8, page 268) Value passing (Code 5-6, page 269) Object passing (code 5-7, page 271) Return Value return (Figure 5-14, page 279, code 5-9, page 280) Object return (code 5-10, page 283) Scope (lifetime of variable) (page 267)

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 ) sub-sub-job1 ) f ( Job.sub-job1. sub-sub-job2 )

Introduction (cont.) 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)

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.

General format, all same Examples f(4) f(3) f(2) f(1) f(0) 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) 1 24 6 1 2 f(4)? General format, all same Terminated condition job division Results collection Result reporting

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

Examples (cont.) f(3) f(2) f(1) f(0) f(1) 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(3) f(2) f(1) f(0) ? y=3 z=0 z=1 y=1 z=3 y=0 z=4 y=2 z=2 f(1)+f(0) 1 3 0 4 f(2)+f(1) 1 1 3 2 f(1) y=1 z=5 1 5 1

Examples (cont.) f(3) f(2) f(1) f(0) 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) 1 1 1 2

Review Idea, working flow Figures 15-1, 15-2, 15-3 (p959-960) Program (program format, static analysis for execution results) Codes 15-4 (p962), 15-7(p969), 15-8 (p970), 15-10 (p978) Design See the discussion on solution in project 2