cp2_020 - CSIS1122B Computer Programming II Module 2 –...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CSIS1122B Computer Programming II Module 2 – Recursion Overview ● What is recursion? ● Why recursion? ● Implementing recursion. ● Improving performance of recursion. ● [O1: Programming Techniques] What is Recursion? ● ( Divide-and-Conquer ) We can call sub- routines (functions) to help us solving some sub-tasks. char x, n; cin >> x >> n; cout << exp(x, n) << endl; // exp(x, n) computes x^n char x, n; cin >> x >> n; cout << exp(x, n) << endl; // exp(x, n) computes x^n // pre: x, n are integers, n >= 0 // post: x^n is returned. double exp(int x, int n) { //... } // pre: x, n are integers, n >= 0 // post: x^n is returned. double exp(int x, int n) { //... } CP2_020:4 Recursive Functions ● There is a special type of functions called recursive functions recursive functions – A recursive function is a function that calls itself during execution. ● e.g., double exp(int x, int n) { if (n == 0) return 1; /* else */ return x * exp(x, n – 1); } double exp(int x, int n) { if (n == 0) return 1; /* else */ return x * exp(x, n – 1) ; } CP2_020:5 Recursion ● When solving a problem, one key technique is to break a big problem into smaller ones. ● Sometimes, at least one sub-problem is very similar to the original problem except that the problem size is smaller. – e.g., to compute exp(x,n), we can compute x * exp(x, n – 1) This is very similar to the original problem, except that the problem size is reduced by one. By doing one extra multiplication, we can use the result of smaller problem to solve a larger problem. CP2_020:6 Why Recursion? ● Recursion is a very powerful problem solving tool ● Once recursive structure of a problem is identified , the corresponding recursive function is very easy to code ● Their code are usually shorter than their iterative counterparts. ● Correctness of recursive function is easier to proof – based on Mathematical Induction. CP2_020:7 How to Use Recursion? ● Key: identify the recursive structure of the problem. ● However, there is no single method to identify the recursive structure of all problems. :-( – We need experience! ● The usual case is that we can express the original problem into similar and smaller ones . CP2_020:8 Recursive Structure Examples ● y n = y n-1 * y ● n! = n * (n-1)! ● gcd(a, b) = gcd(b, a%b) ● F n = F n-1 + F n-2 (Fibonacci number) ● All permutation of some characters = {permutations begin with the 1 st char} U {permutations begin with the 2 nd char} U .. {permutations begin with the last char} CP2_020:9 Base Cases ● Only recursive structure is not enough. We must have base cases – the cases which the solution can be readily available. ● e.g., – y = 1 – 1! = 1, 0! = 1 – gcd(a, 0) = a – F = 0, F 1 = 1 – Permutation of 0 chars is an empty set....
View Full Document

This note was uploaded on 02/05/2012 for the course FBE BUSI1007 taught by Professor Lin during the Spring '11 term at HKU.

Page1 / 59

cp2_020 - CSIS1122B Computer Programming II Module 2 –...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online