1 / 12

Recursive program

This article provides a review of function/method, including its importance, format, working procedure, argument and parameter usage, value and object passing, return values, and scope. It also discusses the introduction of divide-and-conquer and recursive procedures for job division, result collection, and result reporting. Examples and static analysis are provided to illustrate the concepts.

esorenson
Télécharger la présentation

Recursive program

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. Review of Function/Method • Why function/method • To avoid copying the same code in many places • Format (Figure 5-3, page 242) • Call • Declaration (public static, page 527) • Working procedure (Figure 5-4, page 244) • Details of the use of function/method • Argument and parameter (Figure 5-7, page 257) • Multiple arguments passed into multiple parameters (Figure 5-8, page 253) • Value passing (Code 5-6, page 254) • Object passing (code 5-7, page 257) • Return • Value return (Figure 5-14, code 5-9, page 256) • Object return (code 5-10, page 268) • Scope (lifetime of variable) (page 261)

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

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

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

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

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

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

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

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

  12. Review • Idea, working flow • Figures 15-1, 15-2, 15-3 (p919-920) • Program (program format, static analysis for execution results) • Codes 15-4 (p922), 15-7(p929), 15-8 (p930), 15-10 (p937) • Design • See the discussion on solution in project 2

More Related