Recursive program Zhen Jiang West Chester University [email protected].

11
Recursive program Zhen Jiang West Chester University [email protected]

Transcript of Recursive program Zhen Jiang West Chester University [email protected].

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

Recursive program

Zhen Jiang West Chester University

[email protected]

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

Outline Review of function/method Introduction

Design idea Keys

Static Analysis Examples

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

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)

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

Introduction Divide-and-conquer

multi-layer job division (job becomes smaller)

Results collection Result reporting (to

caller)

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

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 )

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

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.)

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

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.

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

Examplesf(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

12624

f(4)?

job divisionResults collection

Result reporting

Terminated condition

General format, all same

f(4) f(3) f(2) f(1) f(0)

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

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)

11

23

f(3) f(2) f(1) f(0)

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

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)

1123

f(3) f(2) f(1) f(0)

y=3z=0z=1

y=2z=2

y=1z=3

1 3

y=0z=4

0 4

?

1

f(1)

y=1z=5

1 5

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

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)

1121

f(3) f(2) f(1) f(0)