0% found this document useful (0 votes)
15 views

Chapter6 1 RecursionUpdated-New

Recursion is a process where a function calls itself to solve sub-problems. A recursive function divides a problem into smaller sub-problems that resemble the original problem until reaching a base case that can be solved directly. This process repeats with each sub-problem calling the function again. While recursion uses more memory than iteration, it can more naturally model some problems and make the program logic easier to understand. Two examples of using recursion to calculate xy and n! (factorial) are provided to illustrate how recursive functions break problems down into base cases.

Uploaded by

Muhd Rzwan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Chapter6 1 RecursionUpdated-New

Recursion is a process where a function calls itself to solve sub-problems. A recursive function divides a problem into smaller sub-problems that resemble the original problem until reaching a base case that can be solved directly. This process repeats with each sub-problem calling the function again. While recursion uses more memory than iteration, it can more naturally model some problems and make the program logic easier to understand. Two examples of using recursion to calculate xy and n! (factorial) are provided to illustrate how recursive functions break problems down into base cases.

Uploaded by

Muhd Rzwan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 10

Simple Recursion

Recursion is where a function calls itself. Concept of recursive function:


A recursive function is called to solve a problem The function only knows how to solve the simplest case of the problem. When the simplest case is given as an input, the function will immediately return with an answer. However, if a more complex input is given, a recursive function will divide the problem into 2 pieces: a part that it knows how to solve and another part that it does not know how to solve.

Principles of Programming - NI2005

Simple Recursion cont


The part that it does not know how to solve always resembles the original problem, but of a slightly simpler version. Therefore, the function calls itself to solve this simpler piece of problem that it does now know how to solve. This is called the recursion step. The recursion step is done until the problem converges to become the simplest case. This simplest case will be solved by the function which will then return the answer to the previous copy of the function. The sequence of returns will then go all the way up until the original call of the function finally return the result.
Principles of Programming - NI2005 2

Simple Recursion cont


Any problem that can be solved recursively can also be solved iteratively (using loop). Recursive functions are slow and takes a lot of memory space compared to iterative functions So why bother with recursion? There are 2 reasons:
Recursion approach more naturally resembles the problem and therefore the program is easier to understand and debug. Iterative solution might not be apparent.

Principles of Programming - NI2005

Example 1 - xy
In this example, we want to calculate x to the power of y (i.e. xy) If we analyze the formula for xy, we could see that xy could be written as (x being multiplied to itself, y times). An example is 24, which can be written as 24 = 2 x 2 x 2 x 2 (in this case, x = 2, y = 4) 24 could also be rewritten as 24 = 21 x 23 where 21 = 2 (i.e the number itself) Therefore, we could divide the problem into two stage: Simplest case: when y = 1, the answer is x Recursive case, we need to solve for x * x(y-1)
Principles of Programming - NI2005 4

Example 1 - xy
#include <stdio.h> double XpowerY(double,int); void main(void) { double power, x; int y; printf("Enter the value of x and y:"); scanf("%lf%d",&x,&y);

power = XpowerY(x,y);
printf("%.2f to the power of %d is %.2f\n\n",x,y,power); }
Principles of Programming - NI2005 5

Example 1 - xy
double XpowerY(double x, int y) { if (y ==1 ) return x; else return x * XpowerY(x, y-1); }
Sample Output: Enter the value of x and y: 2 3 2.0 to the power of 3 is 8.00
Principles of Programming - NI2005 6

Example 2 - Factorial
Analysis:
n!= n * (n-1) * (n-2) * (n-3) * (n-4) * 1 n! could be rewritten as n * (n-1)! Example:
5! = 5 * 4 * 3 * 2 * 1 = 5 * (4)!, where n = 5

Fact: 0! Or 1! is equal to 1

Therefore, we could divide this problem into two stages for n!:
Simplest case: if (n <= 1), answer is 1 Recursive case: we need to solve for n * (n-1)!
Principles of Programming - NI2005 7

Example 2 - Factorial
#include <stdio.h> double fact(double); void main(void) { double n, result; printf("Please enter the value of n:"); scanf("%lf",&n);

result = fact(n);
printf(" %.f! = %.2f\n\n",n,result); }
Principles of Programming - NI2005 8

Tracing a problem
Function Definition Parameter Return value Result

fact(5)

5 * fact(4) to fact(4)
4 * fact(3) to fact(4) 3 * fact(2) to fact(3) 2 * fact(1) to fact(2) Return 1

5 * 24 = 120

fact(4)

4 * 6 = 24

fact(3)

3*2=6

fact(2)

2*1=2

fact(1)

fact(1) =1

Principles of Programming - NI2005

Example 2 - Factorial
double fact(double n) { if (n <= 1) return 1; else return n * fact(n-1); }
Sample Output: Please enter the value of n: 5 5! = 120.00

Principles of Programming - NI2005

10

You might also like