Lab Manual 6 23112021 114139am
Lab Manual 6 23112021 114139am
Lab Manual 6 23112021 114139am
Objectives:
This lab is aimed at introducing students to recursion and recursive algorithms.
1 Recursion
Recursion is a programming technique that allows the programmer to express operations in
terms of themselves. In C++, this takes the form of a function that calls itself. A useful way to
think of recursive functions is to imagine them as a process being performed where one of the
instructions is to "repeat the process". This makes it sound very similar to a loop because it
repeats the same code, and in some ways it is similar to looping. On the other hand, recursion
makes it easier to express ideas in which the result of the recursive call is necessary to complete
the task. Of course, it must be possible for the "process" to sometimes be completed without
the recursive call. One simple example is the idea of building a wall that is ten feet high; if I
want to build a ten foot high wall, and then I will first build a 9 foot high wall, and then add an
extra foot of bricks. Conceptually, this is like saying the "build wall" function takes a height and
if that height is greater than one, first calls itself to build a lower wall, and then adds one a foot
of bricks.
A basic code of recursive function is
void recursion() {
recursion(); /* function calls itself */
}
int main() {
recursion();
}
The Recursion has two parts to solve a problem.
Base Case
The case for which the solution can be stated non-recursively
The case for which the answer is explicitly known.
General Case
The case for which the solution is expressed in smaller version of itself. Also
known as recursive case.
The C++ programming language supports recursion, i.e., a function to call itself. But while using
recursion, programmers need to be careful to define an exit condition from the function;
otherwise it will go into an infinite loop. Recursive functions are very useful to solve many
mathematical problems, such as calculating the power of a number, calculating the factorial of a
number, generating Fibonacci series, etc.
Example#1:
Basic syntax:
if ( n == 0)
return 1; //base case
else
return x * power(x,n-1) //Recursive Step
}
If we pass x=3 and n=4 then what will be34=?
This approach of calculating powers leads to the following recursive definition of the power
function.
Base Case
x0 = 1
Example#2
f (n)=
{ 1 if n=0
n⋅f ( n−1) else
Given a positive integer n, the factorial of n is defined as the product of all integers between 1
and n, including n.
Basic Syntax:
int F(int n)
{
if(n==0)
return 1;
else
return n* F(n-1);
}
fib (n) = 1 if n = 1 or n = 2
= 1 + 1 + 1 + fib(1) + fib(2) = 5
fib(6) =3+5=8
Basic Syntax:
}
If we pass 5 to fib () function then recursive function would be:
It requires more memory to store the automatic variable to solve variable to solve the
memory space i.e., to consume more storage space.
If properly recursion procedure is not checked, then it will create a problem for the
processing and procedure and procedure run out of memory.
In some problems, it is a time consuming process and is not efficient.
It will create indefinite lopping process (non – terminating iteration), if condition not be
applied at the proper.
When a call is made, it takes time to build a stackframe and push it onto the system
stack
Conversely, when a return is executed, the stackframe must be popped from the stack
and the local variables reset to their previous values – this also takes time
++++++++++++++++++++