Business web hosting - Chapter 6 Methods 287 memory space. Iteration normally

Chapter 6 Methods 287 memory space. Iteration normally occurs within a method, so the overhead of repeated method calls and extra memory assignment is omitted. So why choose recursion? Software Engineering Observation 6.10 Any problem that can be solved recursively can also be solved iteratively (nonrecursively). A recursive approach is normally preferred over an iterative approach when the recursive approach more naturally mirrors the problem and results in a program that is easier to understand and debug. Often, a recursive approach can be implemented with few lines of code and a corresponding iterative approach may take large amounts of code. Another reason to choose a recursive solution is that an iterative solution may not be apparent. Performance Tip 6.4 Avoid using recursion in situations requiring performance. Recursive calls take time and consume additional memory. Common Programming Error 6.16 Accidentally having a nonrecursive method call itself either directly or indirectly through another method can cause infinite recursion. Most programming textbooks introduce recursion much later than we have done here. We feel that recursion is a sufficiently rich and complex topic that it is better to introduce it earlier and spread examples of it over the remainder of the text. Figure 6.15 summarizes the recursion examples and exercises in this text. Chapter Recursion examples and exercises 6 Factorial method Fibonacci method Greatest common divisor Sum of two integers Multiply two integers Raising an integer to an integer power Towers of Hanoi Visualizing recursion 7 Sum the elements of an array Print an array Print an array backward Check if a string is a palindrome Minimum value in an array Selection sort Eight Queens Linear search Binary search Quicksort Maze traversal 10 Printing a string input at the keyboard backward Fig. 6.15Summary of recursion examples and exercises in this text (part 1 of 2). g. 6.15 Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01

Leave a Reply