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

Chapter6 Recursion

Uploaded by

Priyansh Jain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Chapter6 Recursion

Uploaded by

Priyansh Jain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Programming for

Problem Solving
(3110003)

Ch. 6 : Recursion

1
Recursion
Recursion is the process by which a
function calls itself.
Example :
void main()
{
int i=5;
printf(“%d”,i);
main();
}

12 February 2021
Advantages
Easy solution for recursively defined
problems.
Complex programs can be easily written
with less code.

12 February 2021
Disadvantages
Recursive code is difficult to understand
and debug.
Terminating condition is must, otherwise
it will go in infinite loop.
Execution speed decrease because of
function call return activity many times.

12 February 2021
Factorial of given no.
#include <stdio.h>
long int fact(int n);
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
printf("Factorial of %d = %ld", n, fact(n));
return 0;
}
long int fact(int n)
{
if (n >= 1)
return n*fact(n-1);
else
return 1;
}

12 February 2021
Fibonacci using recursion
#include <stdio.h>
int fibo(int);
int main()
{
int num;
int result;
printf("Enter the nth number in fibonacci series: ");
scanf("%d", &num);
if (num < 0)
{ printf("Fibonacci of negative number is not possible.\n"); }
else
{ result = fibo(num);
printf("The %d number in fibonacci series is %d\n", num, result); }
return 0;
}

12 February 2021
Fibonacci using recursion…
int fibo(int num)
{
if (num == 0)
{
return 0;
}
else if (num == 1)
{
return 1;
}
else
{
return(fibo(num - 1) + fibo(num - 2));
}
}

12 February 2021
Ackerman function
The Ackermann function is the simplest example of a well-
defined total function which is computable but not primitive
recursive, providing a counterexample to the belief in the
early 1900’s that every computable function was also
primitive recursive .
It grows faster than an exponential function, or even a
multiple exponential function.
The Ackerman function is a well known recursive function
defined for m >= 0, n >= 0 as follows:
A(m,n) =n+1, if m==0
A(m,n) =A(m-1,1), if m>0 and n==0
A(m,n) =A(m-1,A(m,n-1)), if m>0 and n>0

12 February 2021
Ackerman function pgm
#include<stdio.h>
int A(int m, int n);
main()
{
int m,n;
printf("Enter two numbers :: \n");
scanf("%d%d",&m,&n);
printf("\nOUTPUT :: %d\n",A(m,n));
}

int A(int m, int n)


{
if(m==0)
return n+1;
else if(n==0)
return A(m-1,1);
else
return A(m-1,A(m,n-1));
}

12 February 2021
Quick sort

12 February 2021
Quick sort…

12 February 2021
Quick sort…

12 February 2021
Quick sort…

12 February 2021
Quick sort…

12 February 2021
Quick sort…

12 February 2021
Quick sort…

12 February 2021
Quick_Sort(K, LB, UB)

12 February 2021
Quick Sort using recursion
#include<stdio.h>
for(i=0;i<n;i++)
int a[50];
printf("%5d",a[i]);
void qsort(int,int);
return 0;
int split(int,int);
}
int main()
void qsort(int start,int end)
{
{
int n,i;
int s;
printf("How many elements?");
if(start>=end)
scanf("%d",&n);
return;
printf("Enter %d
s=split(start,end);
elements:\n",n);
qsort(start,s-1);
for(i=0;i<n;i++)
qsort(s+1,end);
scanf("%d",&a[i]);
}
qsort(0,n-1);
printf("The resultant array:\n");

12 February 2021
Quick Sort using recursion…
int split(int start,int end)
{
int p=a[start];
int i=start,j=end,temp;
while(i<j)
{
while(a[i]<=p)
i++;
while(a[j]>p)
j--;
if(i<j)
temp=a[i],a[i]=a[j],a[j]=temp;
}
a[start]=a[j];
a[j]=p;
return j;
}

12 February 2021
Merge sort

12 February 2021
Merge sort…

12 February 2021
Merge sort…

12 February 2021
Merge Sort…
Merge Sort

12 February 2021
Function with array as
parameter
z=max(a,n);
In this function call, a is an array of n
integers.
The array a is passed as first argument
while the size of the array is passed
as second argument to the function

12 February 2021
Example
int max(int v[], int size)
{
int i,max;
max=v[0];
for(i=0;i<size;i++)
{
if(v[i]>max)
max=v[i];
}
return(max);
}
12 February 2021
Function with string as
parameter
The strings are treated as character
arrays in C
Definition
void display(char item_name[])
{
}
Declaration
void display(char str[]);
Call
display(names);
12 February 2021
Storage Classes
The storage classes of variable
determines the scope and lifetime of a
variable.
The variable values can be stored in
computer memory or in registers of
CPU.
There are 4-storage classes:
Auto
Register
Static
12 February 2021
Automatic variables
It is declare inside the function.
They are created when function is called
and destroyed automatically when the
function exited.
It is referred local or internal variable.
We can define automatic variable using
‘auto’ keyword.

12 February 2021
Register Variable
A value of variable should be kept into
register, instead of memory.
Register access is much faster than
memory access.
We can define register variable using
‘register’ keyword.

12 February 2021
Static variable
The value of variable persists until the end of
program.
A static variable is initialized only once, when
the program is complied.
We can define static variable using ‘static’
keyword.
Internal static variable:
The value of variable extends up to the end of
function.
External static variable:
External static variable declared outside of all
functions and is variable to all the functions in
12 February 2021
External variables
External variable declared outside the
function.
It is refers to as a global variable.
Global variable can be accessed by any
function in the program.
We can define extern variable using
‘extern’ keyword.

12 February 2021
Preprocessor
The C Preprocessor is a macro
Preprocessor (allows you to define
Macros) that transforms your program
before it is compiled. These
transformations can be inclusion of
header file, macro expansions etc.
All pre processing directives begins with
a # symbol.
Example:
#define PI 3.14
12 February 2021
Directives
Directive Function
#define Defines a macro substitution
#undef Undefines a macro
#include Specifies the files to be included
#ifdef Test for a macro definition
#endif Specifies the end if.
#ifndef Tests whether a macro is not
defined.
#if Test a compile-time condition.
#else Specifies alternatives when if
12 February 2021 test fails
Macro Substitution
Macro substitution has a name and
replacement text, defined with #define
directive.
The Preprocessor simply replaces the name
of macro with replacement text from the
place where the macro is defined in the
source code.
Syntax:
#define identifier string
#define pie 3.14
#define count 100
#define capital “Delhi”
12 February 2021
File inclusion
File inclusive Directories are used to
include user define header file inside C
Program.
File inclusive directory checks included
header file inside same directory (if
path is not mentioned).
File inclusive directives begins
with #include.

12 February 2021
File inclusion…
Syntax:
#include<filename>
#include “filename”
Example:
#include<stdio.h>
#include<string.h>
#include”test.c”

12 February 2021

You might also like