1 / 16

Subroutines

Subroutines. Harder Problems. Examples so far: sum, sum between, minimum straightforward computation small number of intermediate variables single control structure How do we design algorithms for more complicated problems? complex computation many intermediate variables

zacharye
Download Presentation

Subroutines

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

  2. Harder Problems • Examples so far: sum, sum between, minimum • straightforward computation • small number of intermediate variables • single control structure • How do we design algorithms for more complicated problems? • complex computation • many intermediate variables • multiple control structures

  3. Solving Complex Problems • Often it is difficult visualize all the details of a complete solution to a problem • Top-down design • original problem is divided into simpler independent subproblems • successive refinement: these subproblems may require further division into smaller subproblems • continue until all subproblems can be easily solved

  4. Subroutines • Set of instructions to perform a particular computation • subproblems of more complex problems • repeated computation (e.g. different inputs) • may be used in solving other problems • Also called subprograms, methods, functions, and procedures • Subroutines are named • referred to by name inside another program

  5. Declaring subroutines • Adding two integers return valuetype (output) subroutinename parameters(input) int sum_two_numbers(int x, int y){ return x+y; } returnstatement outputvalue

  6. Subroutine: Minimum of two integers int minimum(int x, int y) { if(x < y) return x; else return y; }

  7. Minimum of three numbers • Problem: Design an algorithm to compute the minimum of three integers x, y, and z

  8. Compute minimum of x,y,z Get inputvalues x,y,z Performcomputations Return outputvalue Compute min_tmp =minimum of x and y Compute minimum of min_tmp and z Top-down design

  9. Calling subroutines • Use subroutines for repeated tasks • We can call the minimum subroutine twice to compute the minimum of three integers: int min3(int x, int y, int z) { min_temp = minimum(x,y); return minimum(min_temp,z); }

  10. n!k! (n-k)! C(n,k) = Example: Computing combinations • A combination C(n,k) is the number of ways to select a group of k objects from a population of n distinct objects • n! is the factorial function n! = n(n –1)(n -2)…321 • Problem: Compute C(n,k)

  11. n!k! (n-k)! Compute C(n,k) = Get inputvalues n,k Performcomputations Return outputvalue C(n,k) Compute n! Compute k! Compute (n-k)! Top-down design

  12. Analysis • Computing C(n,k) can be divided into three factorial computations • Compute n! • Compute k! • Compute (n-k)! • Use subroutines for repeated tasks • we can write a factorial subroutine rather than computing C(n,k) directly

  13. factorial(n)factorial(k)  factorial(n-k) combination = Algorithm to compute C(n,k) • Inputs: n, k • Output: combination 1. Get input values for n and k 2. Perform computation: 3. Return combination

  14. Algorithm to compute C(n,k) int combination(int n, int k) { return factorial(n)/ (factorial(k)*factorial(n-k)); }

  15. Exercises You can use any of the subroutines we discussed in class without declaring them • Design an algorithm to compute n! • Suppose we have a subroutine sum_n that computes the sum of the first n integers int sum_n(int n) Use the subroutine sum_n to compute the inclusive sum between two integers

  16. Exercises • Design an algorithm to compute the minimum of four numbers • Design an algorithm to compute the inclusive product between two numbers • for example, the product between 3 and 6 is 3456 = 360

More Related