160 likes | 202 Views
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
E N D
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
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
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
Declaring subroutines • Adding two integers return valuetype (output) subroutinename parameters(input) int sum_two_numbers(int x, int y){ return x+y; } returnstatement outputvalue
Subroutine: Minimum of two integers int minimum(int x, int y) { if(x < y) return x; else return y; }
Minimum of three numbers • Problem: Design an algorithm to compute the minimum of three integers x, y, and z
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
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); }
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)…321 • Problem: Compute C(n,k)
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
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
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
Algorithm to compute C(n,k) int combination(int n, int k) { return factorial(n)/ (factorial(k)*factorial(n-k)); }
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
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 3456 = 360