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

C11 Recursion

programming subject in computer engineering 1st year
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
16 views

C11 Recursion

programming subject in computer engineering 1st year
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 12
Chapter 11 RECURSION Chapter 11: Recursion From the previous chapter, we have seen that a user-defined function in ‘Turbo C can call other functions through a function call. But aside from calling ther functions, a function in a Turbo C program can also call itself. Consider the program segment below: Example 1. factorial (int n) if (0 == 1 |] n == 0) retum 1; else retum (n* factorial(n-1)) ; + ‘The program segment in Example 1 illustrates a function containing a call to itself, The line else return (n* factorial{n-1)}; contains the function call for the factorial function, 11.1 Recursion Defined In Turbo G, the repetitive process by which a function calls itself is called recursion or circular definition. This is a way of defining something in terms of itself A function is said to be recursive if a statement in the body of the function calls the funotion that contains it. A recursive funetion contains the following parts that are placed using an if-else statement: a) Base Case ~ This is the part of the recursive function that is found on the if-clause, This contains the condition that should be satisfied Recursion at one point of execution to terminate the repetitive process done by the recursive function. b) General Case ~ This is part of the recursive function that is found on the else-clause. This contains the function call of the recursive function to itself. NOTE: The repetitive call of the recursive function to itself that is found on the general case should lead towards the condition found on the base case. A simple example of a recursive function is the factorial function which computes the factorial of an integer n (the product of all whole numbers from 1 to 1). This is shown in Example 1. If the factorial function in Example 4 is called with an argument of 1 or 0, the function retums 1; otherwise it returns the product of n* factorial (n-1). To evaluate this expression, factorial () will be called with n -1 as the argument. This process will be repeated until such time that the condition in the if-clause is satisfied, that is, when the value of n becomes 1 or 0. This will be the tracing part assuming that the initial value passes to n = 4. 4" factorial (4-1) 3" factorial (3-1) 2 * factorial (2-1) Recursion us Simplifying the expressions: a 4+ factosiak (a1) 2 3 * factorial) 1 2 tacts 1) 2 is 24. Therefore, the final return value when n= Example 2 . Give the output of the following program when the value entered for a = 5. #include main () inta, b; cliscr(); printf ("Enter a value:"); scant ("%d", 8a); b= solve (a); printf (" The new value is %d", b); getch(); 1) return 2; else retum (solve(a-1) + 2); SAMPLE OUTPUT: Enter a value: 5 The new value is 10 Recursion 6 This is will the tracing part assuming that the value of a = 5: solve (5-1) +2 solve (4-1) +2 solve (3-4) +2 | solve (2-1) +2 ‘Simplifying the expressions: is 10. ‘Therefore, the final return value when a= Example 3 . Give the outpul of the following program when the value entered fora = 4. #include rain () { int a, by alrser(); printf (Enter a value" scant ("ed", 8a); b= solve (a); printf (* The new value is %d", b); getch(); Recursion wr solve (int a) if (@== 1) retum 1; else retum (solve(a-t) + 2); } SAMPLE OUTPUT: Enter a value: 4 The new value Is 7 This is the tracing part assuming that the value of a = 4: solve (4-1) +2 solve (3-1) #2 solve (2-1) #2 Simplifying the expressions: 5 sove(Gi) #257 3 soveT1) +255 a 1 ‘Therefore, the final return value when a= 4 Is 7. Recursion 11.2 Indirect Recursion ‘The examples presented on the earlier part of this chapter are examples of direct recursions. Direct recursions are recursive functions that call itself through a function call directly inside the body of the function. However, recursive functions can also be called indirectly. That is, another function will be defined to make the recursive call. This is called indirect recursion, Consider the example below: Example 4 . Give the output of the following program when the value entered for a = 5. #include snaiin () int ab; olrsor(); printf ("Enter a value:"); scanf("od", &a); b=solve (a); getch(); } solve (int a) be2; printf (°9%c", bs retum by else b= solve2 (at) + 2; printf ("%d ", b); retun bj Recursion no solve? (int a) print ("%d ", b); return b; } else { b= solve (a-1) +2; printf ("%ed ", by; return b; 7 } SAMPLE OUTPUT: Enter a value: 5 7] 246810 Example 4 present a Turbo C program containing indirect recursions. Notice that the functions solve() and solve2() call each other. This is an indirect way of having recursive definition, Assuming that the value entered for the variable a in the main() function is 5. The function solve) will be first called passing 5 as the parameter. solve() and solve2() will alternately call each other until the condition in their if-clause is satisfied. That is, when either solve() or solve2() will be called with the value one (1) as the passed parameter to a. Recursion 120 This is the tracing part assuming that the value of a solve2 (5-1) +2 solve (4-1) +2 solve2 (3-1) +2 solve (2-1) +2 Simplifying the expressions: 8 solye2(S-1) +2=10 prints 10 on the screen 6 solye(a-1) +228 prints 8 on the screen i 4 solye2(5-1) +256 7 2 solya-(2-1) +254 prints 6 on the screen prints 4 on the screen prints 2 on the screen Therefore, when a= 5 the values 2 4 6 8 10 will be printed on screen. Recursion ta REVIEW EXERCISES _ '. What will be the output of the program below, given the values of x and y? ‘Write your answer in the box provided. Show the step-by-step solution. include ‘sample (int a, int b) if (a==1) return 1; else return (b + sample (a-1,b)); } main () int x,y,z; clrscr (); scant ("%d%d", z= sample (x,y): printf ("The value of z is %d", 2); getch (); } when x=5 and y=6 SOLUTION OUTPUT when x=6 and y=8 SOLUTION OUTPUT | Review Exercises 2 2. What will be the output of the program below given the values of x.2Wiite your answer in the box provided. Show the step-by-step solution #include series (int x) if ((x==0) [| (x==1)) return 0; else retum (2 " series (x-1) + x* x) } main () { int x,y; clrscr(); scant (“%d", 8x); y= series (x); printf (* The new value is %d", y); getch (); } when x=5; SOLUTION OUTPUT when x=7; SOLUTION OUTPUT Review Exercises 1 PROG| RAMMING EXERCISES — Write a G program containing a recursive function that will raise a certain integer x to a certain positive integer n. (Example: if x=2 and 53, find 2°=2°2"2=8) Write a program containing the recursive function that will find the following series of numbers 2, 4,3,6,5,8,7... given the value of n from 1 to positive infinity, Write a C program containing a recursive function that will get the whole number quotient result of dividing two integers n and m. (Example if n=4 and m= 3, 4/3 = 1, Output 1) ‘The Fibonacel series: 0, 1, 1, 2,3,5,8,13,21... begins with terms 0 and 1 and has the property that each succeeding term is the sum of the two previous terms, Write a program containing a recursive function that computes and displays the n" Fibonacci number, given the value for n, The Greatest Common Divisor (GCP) of two numbers is the largest integer that evenly divides the two numbers. Write a program containing a recursive function that returns the GCD of two integers. Programming Exercises 14

You might also like