Recursion [ Why is it important?] ~7 easy marks in Exam Paper Seemingly Different Coding Approach In Fact: Strengthen Top-down Thinking Get Mature in - Setting parameters - Function calls - return + work with return value Further Big Jump in programming after learning Debugger Learning Recursion Step 1. Understand Code Step 2. Understand Execution Step 3. Know how to write Repeat practicing the coding approach: showdigits (Notes P.6) + getlargestdigit (Q2) Until get clicked with confidence of coding steps. Don t hurry for Q3 (a)-(e) Required in some other core courses
// return the largest digit in a positive integer n int getlargestdigit(int n) Figure out the signature of the function
// return the largest digit in a positive integer n int getlargestdigit(int n) if (n<10) The easiest case(s) which doesn t need recursion
// return the largest digit in a positive integer n int getlargestdigit(int n) if (n<10) return n; Handle the easiest case(s) -- doesn t need recursion
// return the largest digit in a positive integer n int getlargestdigit(int n) if (n<10) return n;
// return the largest digit in a positive integer n int getlargestdigit(int n) if (n<10) return n; Recursive call (Has return value => save in a variable) getlargesetdigit( );
// return the largest digit in a positive integer n int getlargestdigit(int n) if (n<10) return n; Recursive call (Has return value => save in a variable) int m1; m1=getlargesetdigit(n/10);
// return the largest digit in a positive integer n int getlargestdigit(int n)//56732 => 7 if (n<10) return n; Illustrate with example for further planning. int m1; m1=getlargesetdigit(n/10); //5673 => 7
// return the largest digit in a positive integer n int getlargestdigit(int n)//56732 => 7 if (n<10) return n; Illustrate with example for further planning. int m1; m1=getlargesetdigit(n/10);//5673 => 7 Now, m1 and n%10 (ie. 7 and 2) can decide the final result.
// return the largest digit in a positive integer n int getlargestdigit(int n)//56732 => 7 if (n<10) return n; int m1; m1=getlargesetdigit(n/10);//5673 => 7 if... return m1; return n%10; Illustrate with example for further planning. Now, m1 and n%10 (ie. 7 and 2) can decide the final result.
// return the largest digit in a positive integer n int getlargestdigit(int n)//56732 => 7 X if (n<10) For Q2 and Q3(a)-(e): return n; int m1; X * Do not add any code here (like int m1, return..). Reason: the if-part and -part have different logic. If we force them to share common code, then we add confusing complexity rather than m1=getlargesetdigit(n/10);//5673 giving significant advantage. => 7 if... return m1; return n%10;
// determine whether an integer x contains even digit(s) (0,2,4,6,8) bool containeven(int x) Figure out the signature of the function
// determine whether an integer x contains even digit(s) (0,2,4,6,8) bool containeven(int x) The easiest case(s) which doesn t need recursion
// determine whether an integer x contains even digit(s) (0,2,4,6,8) bool containeven(int x) if (x%2==0) return true; return false; Handle the easiest case(s) -- doesn t need recursion
// determine whether an integer x contains even digit(s) (0,2,4,6,8) bool containeven(int x) return (x%2==0); Simplify from: if (x%2==0) return true; return false;
// determine whether an integer x contains even digit(s) (0,2,4,6,8) bool containeven(int x) return (x%2==0); Recursive call (Has return value => save in a variable)
// determine whether an integer x contains even digit(s) (0,2,4,6,8) bool containeven(int x) return (x%2==0); bool m1=containeven(x/10);
// determine whether an integer x contains even digit(s) (0,2,4,6,8) bool containeven(int x) return (x%2==0); //56732 => true bool m1=containeven(x/10); Illustrate with example for further planning. //5673 => true
// determine whether an integer x contains even digit(s) (0,2,4,6,8) bool containeven(int x) return (x%2==0); //56732 => true bool m1=containeven(x/10); Illustrate with example for further planning. //5673 => true Now, m1 tells true/false about the leading digits. Only n%10 has not been checked yet. Therefore, using m1 and n%10, we can decide the final result.
// determine whether an integer x contains even digit(s) (0,2,4,6,8) bool containeven(int x) return (x%2==0); //56732 => true bool m1=containeven(x/10); Illustrate with example for further planning. //5673 => true if (x%2==0) return true; return m1; Now, m1 tells true/false about the leading digits. Only n%10 has not been checked yet. Therefore, using m1 and n%10, we can decide the final result.
Or we can write the recursive part as: Then simply eliminate m1: // determine whether an integer x contains even digit(s) (0,2,4,6,8) bool containeven(int x) return (x%2==0); if (x%2==0) return true; int m1=containeven(x/10) ; return m1; // determine whether an integer x contains even digit(s) (0,2,4,6,8) bool containeven(int x) return (x%2==0); if (x%2==0) return true; return containeven(x/10);
// show the digits of an integer x reversely (each followed by a space) void showdigitsreverse(int x) Figure out the signature of the function
// show the digits of an integer x reversely (each followed by a space) void showdigitsreverse(int x) The easiest case(s) which doesn t need recursion
// show the digits of an integer x reversely (each followed by a space) void showdigitsreverse(int x) cout << x << " "; Handle the easiest case(s) -- doesn t need recursion
// show the digits of an integer x reversely (each followed by a space) void showdigitsreverse(int x) cout << x << " "; Recursive call (no return value here)
// show the digits of an integer x reversely (each followed by a space) void showdigitsreverse(int x) cout << x << " "; showdigitsreverse(x/10); Recursive call (no return value here)
// show the digits of an integer x reversely (each followed by a space) void showdigitsreverse(int x) cout << x << " "; showdigitsreverse(x/10); //56732 => 2 3 7 6 5 Illustrate with example for further planning. //5673 => 3 7 6 5
// show the digits of an integer x reversely (each followed by a space) void showdigitsreverse(int x) cout << x << " "; showdigitsreverse(x/10); //56732 => 2 3 7 6 5 Illustrate with example for further planning. //5673 => 3 7 6 5 In the code, the recursive call can output 3 7 6 5. Only n%10 has not been handled yet. Since the required output is 2 3 7 6 5, we can easily decide what is to be done and when.
// show the digits of an integer x reversely (each followed by a space) void showdigitsreverse(int x) cout << x << " "; cout << x%10; showdigitsreverse(x/10); //56732 => 2 3 7 6 5 Illustrate with example for further planning. //5673 => 3 7 6 5 In the code, the recursive call can output 3 7 6 5. Only n%10 has not been handled yet. Since the required output is 2 3 7 6 5, we can easily decide what is to be done and when.