140 likes | 159 Views
Learn about recursion and how to split a problem into smaller problems using recursion. Includes examples and solutions.
E N D
Chapter 10: Recursion CS 201 Program Design with C Department of CS, Montana State University Mahmud Shahriar Hossain
Splitting a Problem into Smaller Problems • The problem must be divisible • Starting • Terminating condition • Progress • Example: 3 X 4 = ??!!??
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
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
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
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.
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”
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
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
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; }
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; }