/
Recursion (II) H&K Chapter 10 Instructor –  Gokcen   Cilingir Recursion (II) H&K Chapter 10 Instructor –  Gokcen   Cilingir

Recursion (II) H&K Chapter 10 Instructor – Gokcen Cilingir - PowerPoint Presentation

aaron
aaron . @aaron
Follow
342 views
Uploaded On 2019-11-01

Recursion (II) H&K Chapter 10 Instructor – Gokcen Cilingir - PPT Presentation

Recursion II HampK Chapter 10 Instructor Gokcen Cilingir Cpt S 121 July 25 2011 Washington State University Recall recursive functions A function that calls itself is said to be recursive Here are examples of famous recursively defined functions in math factorial and ID: 761980

case recursive int ans recursive case ans int multiply function amp base simple recursion palindrome statement source study problem

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Recursion (II) H&K Chapter 10 Instru..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Recursion (II)H&K Chapter 10 Instructor – Gokcen CilingirCpt S 121 (July 25, 2011)Washington State University

Recall: recursive functionsA function that calls itself is said to be recursive. Here are examples of “famous” recursively defined functions in math: factorial and fibonacci Base case/step Recursive case/step The ability to invoke itself enables a recursive function to be repeated with different parameter values

C. Hundhausen, A. O’Fallon3Recall: recursion Problems that may be solved using recursion have these attributes:One or more simple cases have a straightforward, non-recursive solutionThe other cases may be defined in terms of problems that are closer to the simple casesThrough a series of calls to the recursive function, the problem eventually is stated in terms of the simple casesi mage source: H&K, figure 10.1

Example 1Write a function that performs multiplication through addition with the prototype: int multiply(int m, int n); Let’s recursively define multiply: base case: multiply (m,1) is m recursive case: multiply (m, n) is n + multiply (m, n-1)

Example 1 (cont’d)Here is the recursive C implementation of mult: int multiply (int m, int n){ int ans; if (n == 1) ans = m; // base case else ans = m + multiply (m, n – 1); // recursive case return ans ; }

Tracing a recursive function image source: H&K, figure 10.5

Alternative tracing: execute the following codeint multiply ( int m, int n){ int ans; printf("Function called with m: %d, n: %d\n", m, n); if (n == 1) //base case { ans = m; } else // recursive case { ans = m + multiply (m, n - 1); } printf (" ans is: %d\n", ans ); return ans ; }

Case study 1Problem statement: A palindrome consists of a word or deblanked, unpunctuated phrase that is spelled exactly the same when the letters are reversed. Write a recursive function (in C) that returns a value of 1 if its string argument is a palindrome. Notice that in palindromes such as madam i'm adam (madamimadam), level, deed, and sees, the first letter matches the last, the second matches the next-to-last, and so on.

Case study 2Problem statement: Implement the recursive version of binary search algorithm.

10ReferencesJ.R. Hanly & E.B. Koffman, Problem Solving and Program Design in C (6th Ed.), Addison-Wesley, 2010P.J. Deitel & H.M. Deitel, C How to Program (5th Ed.), Pearson Education , Inc., 2007.