Download presentation
Presentation is loading. Please wait.
1
Chapter 10: Recursion CS 201 Program Design with C Department of CS, Montana State University Mahmud Shahriar Hossain
2
2 Splitting a Problem into Smaller Problems The problem must be divisible Starting Terminating condition Progress Example: 3 X 4 = ??!!??
3
3 3 X 4 = ??? #include 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; } Output: 3 X 4 = 12 #include 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
4
4 How many ‘s’
5
5 #include 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
6 Trace of Function multiply ( 6 X 3 = ???) #include 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
7 Function reverse_input_words() #include #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
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
9 Sequence of Events for Trace of reverse_input_words(3)
10
10 Trace recursive function multiply() #include 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; } 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 //printf to keep track
11
11 Recursive factorial function #include 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 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
12 Trace of factorial(3); #include 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
13 Recursive Function fibonacci #include // 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
14 Thank You
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.