Chapter6 1 Recursion Version3.0 Question
-
Upload
muhd-rzwan -
Category
Documents
-
view
214 -
download
0
Transcript of Chapter6 1 Recursion Version3.0 Question
-
7/29/2019 Chapter6 1 Recursion Version3.0 Question
1/12
Principles of Programming - NI2005 1
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 isgiven 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 2pieces: a part that it knows how to solve and
another part that it does not know how to solve.
-
7/29/2019 Chapter6 1 Recursion Version3.0 Question
2/12
Principles of Programming - NI2005 2
Simple Recursion cont
The part that it does not know how to solve alwaysresembles the original problem, but of a slightly simplerversion.
Therefore, the function calls itself to solve this simplerpiece of problem that it does now know how to solve.
This is called the recursion step.The recursion step is done until the problem convergesto become the simplest case.
This simplest case will be solved by the function whichwill then return the answer to the previous copy of the
function.
The sequence ofreturns will then go all the way upuntil the original call of the function finally return theresult.
-
7/29/2019 Chapter6 1 Recursion Version3.0 Question
3/12
Principles of Programming - NI2005 3
Simple Recursion cont
Any problem that can be solved recursively canalso be solved iteratively (using loop).
Recursive functions are slow and takes a lot ofmemory space compared to iterative functions
So why bother with recursion? There are 2reasons:
Recursion approach more naturally resembles the
problem and therefore the program is easier to
understand and debug.
Iterative solution might not be apparent.
-
7/29/2019 Chapter6 1 Recursion Version3.0 Question
4/12
Principles of Programming - NI2005 4
Example 1 - 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
-
7/29/2019 Chapter6 1 Recursion Version3.0 Question
5/12
Principles of Programming - NI2005 5
Example 1 - Factorial
#include 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);}
-
7/29/2019 Chapter6 1 Recursion Version3.0 Question
6/12
Principles of Programming - NI2005 6
Example 1 - Factorial
double fact(double n){
if (n
-
7/29/2019 Chapter6 1 Recursion Version3.0 Question
7/12
Principles of Programming - NI2005 7
Tracing a problem - factorial
Fact (4)
Fact (4)
= 4 * Fact (3)Fact (3)
= 3 * Fact (2)
Fact (2)
= 2 * Fact (1)
Fact (2)
2 * 1
Fact (3)
3 * 2
Fact (1)
= return 1
Fact (4)
4 * 6
double fact(double n){
if (n
-
7/29/2019 Chapter6 1 Recursion Version3.0 Question
8/12
Principles of Programming - NI2005 8
Example 2 : xy
In this example, we want to calculate x to the powerof y (i.e. xy)
If we analyze the formula for xy, we could see that xycould be written as (x being multiplied to itself, ytimes).
An example is 24, which can be written as24 = 2 x 2 x 2 x 2 (in this case, x = 2, y = 4)
24 could also be rewritten as
24 = 21x 23 where 21 = 2 (i.e the number itself)
Therefore, we could divide the problem into twostage:
Simplest case: when y = 1, the answer is x
Recursive case, we need to solve for x * x(y-1)
-
7/29/2019 Chapter6 1 Recursion Version3.0 Question
9/12
Principles of Programming - NI2005 9
Example 2 : xy
#include double XpowY(double,int);
void main(void){
double power, x;int y;
printf("Enter the value of x and y:");scanf("%lf%d",&x,&y);
power = XpowY(x,y);
printf("%.2f to the power of %d is %.2f\n\n",x,y,power);}
-
7/29/2019 Chapter6 1 Recursion Version3.0 Question
10/12
Principles of Programming - NI2005 10
Example 2 : xy
double XpowY(double x, int y){
if (y ==1 )
return 1;elsereturn x * XpowY(x, y-1);
}
Sample Output:
Enter the value of x and y: 2
3
2.0 to the power of 3 is 8.00
-
7/29/2019 Chapter6 1 Recursion Version3.0 Question
11/12
Tracing a problem - XpowY
Principles of Programming - NI2005 11
?
-
7/29/2019 Chapter6 1 Recursion Version3.0 Question
12/12
Summary
Recursive definitionExample of recursive code
Factorial
X power of yTracing technique applied in recursive
Principles of Programming - NI2005 12