1 / 15

CMPS 1371 Introduction to Computing for Engineers

RECURSION. CMPS 1371 Introduction to Computing for Engineers. Recursion. Simple definition: When a function calls itself Why: Simple reason: often a problem can reduce to exactly the same problem but on a slightly simpler task. Recursion. Examples: Factorial

mona-joyner
Télécharger la présentation

CMPS 1371 Introduction to Computing for Engineers

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. RECURSION CMPS 1371Introduction to Computing for Engineers

  2. Recursion • Simple definition: • When a function calls itself • Why: • Simple reason: often a problem can reduce to exactly the same problem but on a slightly simpler task

  3. Recursion Examples: Factorial n! = (n)(n-1)(n-2)...(3)(2)(1)‏ Fibonacci Fn = Fn-1 + Fn-2

  4. A "stack" Last one in is first one out (LIFO) Takes the element on top regardless of size Not too hard to get to recent ones… • A stack is used for functions to get space for it's arguments, internal data, and it's return location • Each call to a function gets the room it needs on the stack ("pushed" onto the stack). • Allows calling function over and over…

  5. Recursion vs Iteration Iteration for loops and while loops that solve a problem by iterating through steps Recursion functions that call themselves, moving towards one or more base cases

  6. Sum function As you may know, finding the sum of a vector in MATLAB is easy—we can use the built-in MATLAB function sum(): << x = [1 2 3]; << sum(x)‏ ans = 6 However, it is an important programming skill to know how to find the sum of a vector (or an array) without using a built-in function, because many languages do not have such a function.

  7. Iteration for sum function function result = sum1(x)‏ for i=1:length(x) result = x(i) + result; end Adds each element to result

  8. Recursion for sum function There are only three possibilities for a vector x: x is an empty vector, which has the sum 0 x is really just a single element—hence xisalready the sum of the vector x xis vector with at least two elements, and can thus be divided into two parts; the first element, x(1) the rest of the vector, x(2:end)

  9. Recursion for sum function function result = sum2(x) if ( isempty(x) )‏ result = 0; elseif ( length(x) == 1)‏ result = x; else result = x(1) + sum2 (x(2:end)); end Calls the function over again till it reads all elements

  10. Factorial Function Create a function to calculate the factorial amount Result should be as follows: >> fact(5)‏ ans = 120 5! = (5)(4)(3)(2)(1)

  11. Factorial Recursion Try this: first make a new function “fact” function y = fact(x)‏ if x==1 y=1; else y=x*fact(x-1); end This function calls itself. It’s recursive. How does this work?

  12. Palindromes Palindrome is a word or sentence that is spelled the same forwards and backwards Examples: level radar toot i prefer pi Napolean's classic lament: able was i ere i saw elba • eye • kayak • racecar

  13. Palindromes Create a function to determine if a word is a palindrome How would we do it recursively? If start and end are the same and the middle is a palindrome, then is true What are the terminating conditions? if length == 1, yes if length == 2, then if strcmp(str(1),str(2)), then yes radar

  14. Palindrome code function ans = isPal(str) % is str a palindrome? % isPal(‘ enter your word ’) str = lower(str); if length(str) < 2 ans = true; disp('your word is a palindrome') elseif str(1) ~= str(end) ans = false; disp('your word is not a palindrome') else ans = isPal(str(2:end-1)); end

  15. Recursion Characteristics • There are three necessary characteristics of all recursive functions • There must be a terminating condition to stop the process • The function must call a clone of itself • The parameters to that call must move the function toward the terminating condition • A recursive function does not really call itself because it passes different parameters to the function

More Related