0
Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Standard text messaging rates apply

# Removal Of Recursion

6,024

Published on

This presentation gives details about removing recursion

This presentation gives details about removing recursion

1 Comment
2 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• nc

Are you sure you want to  Yes  No
Views
Total Views
6,024
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
151
1
Likes
2
Embeds 0
No embeds

No notes for slide

### Transcript

• 1.
• 2. <ul><li>The function which call itself (In function body ) again and again is known as recursive function . This function will call itself as long as the condition is satisfied. </li></ul><ul><li>This recursion can be removed by two ways: </li></ul><ul><li>1. Through Iteration. </li></ul><ul><li>2. Through Stack </li></ul>
• 3. <ul><li>A simple program of factorial through recursion: </li></ul>
• 4. <ul><li>/*Find the factorial of any number*/ </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>main() </li></ul><ul><li>{ </li></ul><ul><li>int n, value; </li></ul><ul><li>printf(“Enter the number”); </li></ul><ul><li>scanf(“%d”,&n); </li></ul><ul><li>if(n<0) </li></ul><ul><li>printf(“No factorial of negative number”); </li></ul><ul><li>else </li></ul><ul><li>if(n==0) </li></ul><ul><li>printf(“Factorial of zero is 1”); </li></ul>
• 5. <ul><li>else </li></ul><ul><li>{ </li></ul><ul><li>value=factorial(n); /*function for factorial of number*/ </li></ul><ul><li>printf(“Factorial of %d= %d”,n,value); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>factorial (int k) </li></ul><ul><li>{ int fact=1; </li></ul><ul><li>ifk>1) </li></ul><ul><li>fact=k*factorial(k-1); /*recursive function call*/ </li></ul><ul><li>return (fact); </li></ul><ul><li>} </li></ul>
• 6. <ul><li>Same thing can be replaced with Iteration as </li></ul>/*Find the factorial of any number*/ #include<stdio.h> main() { int n, value; printf(“Enter the number”); scanf(“%d”,&n); if(n<0) printf(“No factorial of negative number”); else if(n==0) printf(“Factorial of zero is 1”);
• 7. <ul><li>else </li></ul><ul><li>{ </li></ul><ul><li>value=factorial(n); /*function for factorial of number*/ </li></ul><ul><li>printf(“Factorial of %d= %d”,n,value); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>int factorial(int no) </li></ul><ul><li>{ </li></ul><ul><li>int I,fact=1; </li></ul><ul><li>for(i=no;i<1;i--) </li></ul><ul><li>fact=fact*I; </li></ul><ul><li>return fact; </li></ul><ul><li>} </li></ul>
• 8. <ul><li>Suppose P is a recursive procedure . The translation of recursive procedure P into a non recursive procedure work as follows: </li></ul><ul><li>First of all, one defines: </li></ul><ul><li>1. A stack STPAR for each parameter PAR </li></ul><ul><li>2. A stack STVAR for each local variable VAR </li></ul><ul><li>3. A local variable ADD and a stack STADD to hold return address. </li></ul>
• 9. <ul><li>The algorithm which translates the recursive procedure P into a non recursive procedure follows. It consist of three parts: </li></ul><ul><li>1. Preparation </li></ul><ul><li>2. Translating each recursive call P in procedure P. </li></ul><ul><li>3. Translating each return in procedure P. </li></ul>
• 10. <ul><li>Preparation </li></ul><ul><li>(a) define a stack STPAR for each parameter PAR, a stack STVAR for each local variable VAR, and a local variable ADD and a stack STADD to hold return address. </li></ul><ul><li>(b) Set TOP = NULL </li></ul><ul><li>2. Translation of “step K.call P.” </li></ul><ul><li>(a) Push the current values of the parameters and local variable onto the appropriate stacks, and push the new return address [step ] K+1 onto STADD. </li></ul>
• 11. <ul><li>(b) reset the parameters using the new argument values. </li></ul><ul><li>(c) go to step 1.[The beginning of procedure P] </li></ul><ul><li>3. Translation of “Step J.return” </li></ul><ul><li>(a) if STADD is empty, then return.[control is return to the main program]. </li></ul><ul><li>(b) Restore the top values of the stacks.That is, set the parameters and local variables equal to the top values on the stacks, and set ADD equal to the top value on the STADD. </li></ul>
• 12. <ul><li>(c) Go to the step ADD. </li></ul><ul><li>4. Step L. return </li></ul>