Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Recursion.

Similar presentations


Presentation on theme: "Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Recursion."— Presentation transcript:

1 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Recursion

2 7-2 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Recursion – The process of solving a problem by reducing it to smaller versions of itself – Example: Factorial

3 7-3 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Recursive algorithm – An algorithm is called recursive if it solves a problem by reducing the problem to smaller versions of itself – A recursive algorithm can be implemented using a recursive function

4 7-4 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Recursive function – A function that calls itself – What is the danger? – we need to avoid infinite recursion – A recursive function must have – recursive call(s) to the function with a smaller instance of the problem – one or more base cases to stop recursion

5 7-5 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Finding a recursive solution – A recursive solution to a problem must be written carefully – The idea is for each successive recursive call to take us one step closer to the base case (a situation in which the problem can easily be solved)

6 7-6 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley General format for many recursive functions if (some easily-solved condition) // base case solution statement(s); else // general case recursive function call(s);

7 7-7 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Example – Write a recursive function Fact() to find the factorial of n. – What should Fact(4) return? – What can be a good base case that represents an easily-solved situation? – What can be a good general case that takes us closer to the base case?

8 7-8 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Example int Fact (int n) { if (n == 1) // base case return 1; else // general case return n * Fact(n-1); } // this is an example of tail recursion

9 7-9 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Example – Trace of calls n = 4 n = 3 n = 2 Call 1: Fact(4) Call 2: Fact(3) n = 1 Call 3: Fact(2) Call 4: Fact(1) n = 1 Returns 1 Returns 2 * Fact(1) = 2 * 1 = 2 Returns 3 * Fact(2) = 3 * 2 = 6 Returns 4 * Fact(3) = 4 * 6 = 24

10 7-10 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Assignment – Write a recursive function Power() to calculate x n, where n is a non-negative integer. – Extend the Power() function so that it works for any integer n.

11 7-11 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Empty base case – Recursive functions can have empty base cases – What action should take place? – What should be the return type? – Write a recursive function that will display all integers from n down to 1.

12 7-12 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Iteration or Recursion? – Key factors – nature of the problem – efficiency – Every recursive call has its own set of parameters and local variables – time and space overhead associated with executing a recursive function – Certain problems are inherently recursive and a recursive solution is the most natural one

13 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Command Line Arguments

14 7-14 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Command Line Arguments – Arguments can be passed to any program being executed from a command-line based OS like DOS or UNIX – UNIX Example: cp fileA fileB – cp is the name of the program (command) to be executed – fileA and fileB are command-line arguments – In C/C++, the program can be written to accept command-line-arguments

15 7-15 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C/C++ syntax – The following syntax is used in the main() function to capture command-line arguments int main(int argc, char *argv[]) – In this case, the operating system passes in two parameters to the function – argc is an integer specifying the number of command- line arguments – argv is an array of pointers where each pointer points to one of the command-line arguments stored as a C- type string

16 7-16 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C++ syntax (Cont..) – In some systems (like UNIX), argv[0] points to a string that is the entire path to the program (executable) including its name – In other systems, argv[0] points to garbage – You can use any argv[i] like a C-type string – In C++, if the program is not written to accept command-line arguments, it is not necessary to specify the parameters in the argument list of the function signature

17 7-17 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Example #include using namespace std; void main(int argc, char *argv[]) { string fname, lname; fname = argv[1]; lname = argv[2]; cout << argc << " " << argv[1] << " " << argv[2] << endl; } g++ -o comlinarg comlinarg.cpp./comlinarg John Malkovich 3 John Malkovich


Download ppt "Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Recursion."

Similar presentations


Ads by Google