1 / 14

Recursion in Program Design

Learn about recursion and how to split a problem into smaller problems using recursion. Includes examples and solutions.

olap
Download Presentation

Recursion in Program Design

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 10: Recursion CS 201 Program Design with C Department of CS, Montana State University Mahmud Shahriar Hossain

  2. Splitting a Problem into Smaller Problems • The problem must be divisible • Starting • Terminating condition • Progress • Example: 3 X 4 = ??!!??

  3. 3 X 4 = ??? #include <stdio.h> int main(){ int m=3, n=4, result = 0; for (int i=0; i<n; i++){ result+=m; } printf ("%dX%d = %d\n",m,n,result ); return 0; } #include <stdio.h> int multiply (int m, int n){ int result=0; if (n==1) result= m; else result = m+multiply(m,n-1); return result; } int main(){ int m=3, n=4; printf ("%dX%d = %d\n",m,n, multiply(m,n)); return 0; } Iterative Solution Recursive Solution Output: 3 X 4 = 12

  4. How many ‘s’

  5. How many ‘s’ #include <stdio.h> int count (char ch, char *str){ int ans=0; if (str[0]=='\0') ans=0; else if (ch==str[0]) ans=1+count(ch, &str[1]); else ans = count (ch, &str[1]); return ans; } int main(){ char *str="Mississipi sassafras"; printf ("Total Number of s in the string = %d\n", count('s',str)); return 0; } Output: Total Number of s in the string = 8

  6. Trace of Function multiply ( 6 X 3 = ???) #include <stdio.h> int multiply (int m, int n){ int ans=0; if (n==1) ans= m; else ans = m+multiply(m,n-1); return ans; } int main(){ int m=6, n=3; printf ("%dX%d = %d\n",m,n, multiply(m,n)); return 0; } SAVES ALL THE VALUES ON THE TOP OF STACK

  7. Function reverse_input_words() #include <stdio.h> #define WORDSIZ 10 void reverse_input_words(int n){ char word[WORDSIZ]; if (n<=1){ scanf("%s", word); printf("%s\n", word); } else{ scanf("%s", word); reverse_input_words(n-1); printf("%s\n",word); } } int main(){ reverse_input_words(3); return 0; } Take 3 strings as inputs from the user and output them in reverse order.

  8. reverse_input_words(3) void reverse_input_words(int n){ char word[WORDSIZ]; if (n<=1){ scanf("%s", word); printf("%s\n", word); } else{ scanf("%s", word); reverse_input_words(n-1); printf("%s\n",word); } } int main(){ reverse_input_words(3); return 0; } Input: “bits and bytes”

  9. Sequence of Events for Trace of reverse_input_words(3)

  10. Trace recursive function multiply() #include <stdio.h> int multiply (int m, int n){ int ans; printf("Entering multiply with m=%d, n=%d\n", m, n); if (n==1) ans= m; else ans = m+multiply(m,n-1); printf("multiply (%d, %d) = %d\n", m, n, ans); return ans; } int main(){ int m=8, n=3; multiply(m, n); scanf("%d"); return 0; } //printf to keep track //printf to keep track Output: Entering multiply with m=8, n=3 Entering multiply with m=8, n=2 Entering multiply with m=8, n=1 multiply (8,1)=8 multiply (8,2)=16 multiply (8,3)=24

  11. Recursive factorial function #include <stdio.h> int factorial (int n){ int ans; if (n==0) ans = 1; else ans = n * factorial(n-1); return ans; } int main(){ printf ( "5!= %d \n", factorial(5)); return 0; } #include <stdio.h> int main(){ int i, product = 1; for ( i=5; i>1 ; -- i ){ product = product * i; } printf ( "5!= %d \n", product); scanf("%d"); return 0; } Iterative solution Recursive solution

  12. Trace of factorial(3); #include <stdio.h> int factorial (int n){ int ans; if (n==0) ans = 1; else ans = n * factorial(n-1); return ans; } int main(){ int fact = factorial(3); printf ( “3!= %d \n", fact ); return 0; }

  13. Recursive Function fibonacci #include <stdio.h> // Compute n-th fibonacci int fibonacci(int n){ int ans; if (n==1 || n==2) ans=1; else ans = fibonacci(n-2) + fibonacci(n-1); return ans; } int main(){ printf ( " The 5th fibo nimber: %d \n", fibonacci(5)); scanf("%d"); return 0; }

  14. Thank You

More Related