Question: Compare the following two functions and explain how they differ to achieve the same result, i.e. what technique is used in the second example, how
Compare the following two functions and explain how they differ to achieve the same result, i.e. what technique is used in the second example, how this technique works, and why it is important. Do NOT trace the code but rather explain the ideas the code illustrates.
| 1) | 2) |
| int fib (int n) { if (n <= 1) return n; return fib(n-1) + fib(n-2); } | int fib (int n) { helper(n, 0, 1); } int helper (int n, int a, int b) { if (n == 0) return a; if (n == 1) return b; return helper(n-1, b, a+b); } |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
