Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.

Slides:



Advertisements
Similar presentations
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 10: Recursion Problem Solving & Program Design in C Sixth Edition.
Advertisements

Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
Recursive methods. Recursion A recursive method is a method that contains a call to itself Often used as an alternative to iteration when iteration is.
Recursion H&K Chapter 10 Instructor – Gokcen Cilingir Cpt S 121 (July 21, 2011) Washington State University.
Chapter 10 Recursion Instructor: alkar/demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe.
Recursion Chapter 7. Spring 2010CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn how.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 14: Recursion by.
Recursion. Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn about recursive algorithms Lecture.
Recursion. 2 CMPS 12B, UC Santa Cruz Solving problems by recursion How can you solve a complex problem? Devise a complex solution Break the complex problem.
Fall 2007CS 2251 Recursion Chapter 7. Fall 2007CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Chapter 11 Structure and Union Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
Chapter 10: Recursion CS 201 Program Design with C Department of CS, Montana State University Mahmud Shahriar Hossain.
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Unit 7 1 Unit 7: Recursion H Recursion is a fundamental programming technique that can provide an elegant solution to a certain kinds of problems H In.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10: Recursion Problem Solving and Program Design in C 5th Edition.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Recursion Chapter Nature of Recursion t Problems that lend themselves to a recursive solution have the following characteristics: –One or more.
Chapter 2 Recursion: The Mirrors CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 15: Recursion Starting Out with Java: From Control Structures.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 14: Recursion.
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Chapter 12 Recursion, Complexity, and Searching and Sorting
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
© 2012 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 9: Recursion Problem Solving & Program Design in C Seventh.
Recursion CMPE231, Spring 2012 Aleaxander G. Chefranov 1.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Chapter 8 Recursion Modified.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
Chapter 13 Programming in the Large Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 14 Recursion.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
 Prentice Hall. All rights reserved. 1 Recursion (Section 7.13 & Exercise7.40 from ed.3) (Sections 6.15, 6.16 from ed.1) Many slides modified.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 15 * Recursive Algorithms.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
Chapter 12 Text and Binary File Processing Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
chap10 Chapter 10 Recursion chap10 2 Recursive Function recursive function The recursive function is a kind of function that calls.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Program Development and Design Using C++, Third Edition
1 Dr. Chow-Sing LinRecursion - CH 10 Problem Solving and Program Design in C Chapter 9 Recursion Chow-Sing Lin.
Chapter 9 Recursion. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe recursive function is –a.
Recursion Topic 5.
Topic 6 Recursion.
Chapter 15 Recursion.
CprE 185: Intro to Problem Solving (using C)
Chapter 10 Recursion Instructor: Yuksel / Demirer.
Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng.
Recursion Chapter 12.
Chapter 15 Recursion.
Data Structures Recursion CIS265/506: Chapter 06 - Recursion.
Java Software Structures: John Lewis & Joseph Chase
Chapter 10 Recursion.
Chapter 12 Recursion (methods calling themselves)
Chapter 10: Recursion Problem Solving and Program Design in C 5th Edition by Jeri R. Hanly and Elliot B. Koffman.
Dr. Sampath Jayarathna Cal Poly Pomona
Presentation transcript:

Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Outline 10.1 THE NATURE OF RECURSION 10.2 TRACING A RECURSIVE FUNCTION 10.3 RECURSIVE MATHMETICAL FUNCTIONS 10.4 RECURSIVE FUNCTIONS WITH ARRAY AND STRING PARAMETERS –CASE STUDY FINDING CAPITAL LETTERS IN A STRING –CASE STUDY RECURSIVE SELECTION SORT 10.5 PROBLEM SOLVING WITH RECURSION –CASE STUDY OPERATIONS ON SETS 10.6 A CLASSIC CASE STUDY IN RECURSION –TOWERS OF HANOI 10.7 COMMON PROGRAMMING ERRORS

Copyright ©2004 Pearson Addison-Wesley. All rights reserved THE NATURE OF RECURSION Recursive function –Function that calls itself or that is part of a cycle in the sequence of function calls Simple case –Problem case for which a straightforward solution is known Recursive solution characteristics –One or more simple cases of the problem have a straightforward, nonrecursive solution –The other cases can be redefined in terms of problems that are closer to the simple cases –By applying this redefinition process every time the recursive function is called, eventually the problem is reduced entirely to simple cases, which are relatively easy to solve

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-4 Recursive Algorithm if this is a simple case solve it else redefine the problem using recursion

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-5 Figure 10.1 Splitting a Problem into Smaller Problems

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-6 EXAMPLE 10.1 Figure 10.2 implements multiplication as the recursive C function multiply that returns the product m x n of its two arguments. The body of function multiply implements the general form of a recursive algorithm. The simplest case is reached when the condition n==1 is true.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-7 Figure 10.2 Recursive Function multiply

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-8 EXAMPLE 10.2 Develop a function to count the number of times a particular character appears in a string. Figure 10.3 shows thought process that fits into our generic else clause.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-9 Figure 10.3 Thought Process of Recursive Algorithm Developer

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure 10.4 Recursive Function to Count a Character in a String

Copyright ©2004 Pearson Addison-Wesley. All rights reserved TRACING A RECURSIVE FUNCTION Two types of tracing the execution of a recursive function –Tracing a recursive function that returns a value –Tracing a void function that is recursive

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Tracing a recursive function that returns a value Activation frame –Representation of one call to a function Figure 10.5 shows three calls to function multiply.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure 10.5 Trace of Function multiply

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Tracing a void function that is recursive Function reverse_input_words in Fig.10.6 is a recursive module that takes n words of input and prints them in reverse order. Terminating condition –A condition that is true when a recursive algorithm is processing a simple case

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure 10.6 Function reverse_input_words

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure 10.7 Trace of reverse_input_words(3) When the Words Entered are "bits" "and" "bytes"

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure 10.8 Sequence of Events for Trace of reverse_input_words(3)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Parameter And Local Variable Stacks (1/5) C uses the stack data structure to keep track of the values of n and word at any given point. After first call to reverse_input_words nword 3 ?

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Parameter And Local Variable Stacks (2/5) Before the second call to reverse_input_words n word 3 bits After second call to reverse_input_words n word 3bits 2?

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Parameter And Local Variable Stacks (3/5) Before the third call to reverse_input_words n word bits 3 2and After third call to reverse_input_words n word 3bits 2 and 1?

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Parameter And Local Variable Stacks (4/5) During this execution of the function, the word “bytes” is scanned and stored in word, and “bytes” is echo printed immediately because n is 1 (a simple case) n word 3bits 2 and 1bytes The function return pops both stacks After first return nword 3bits 2and

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Parameter And Local Variable Stacks (5/5) Because control is returned to a printf call, the value of word at the top of the stack is then displayed. Another return occurs, poping the stacks again. After second return nword 3bits

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Implementation of Parameter Stacks in C System stack –Area of memory where parameters and local variables are allocated when a function is called and deallocated when the function returns When and how to trace recursive functions –During algorithm development, it is best to trace a specific case simply by trusting any recursive call to return a correct value based on the function purpose. –Figure 10.9 shows a self-tracing version of function multiply as well as output generated by the call.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure 10.9 Recursive Function multiply with Print Statements to Create Trace and Output from multiply(8, 3)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure 10.9 Recursive Function multiply with Print Statements to Create Trace and Output from multiply(8, 3) (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved RECURSIVE MATHMETICAL FUNCTIONS Many mathmatical functions can be defined recursively. For example –The factorial of a number n (n!)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Recursive factorial Function

Copyright ©2004 Pearson Addison-Wesley. All rights reserved EXAMPLE 10.4 Figure shows the trace of fact=factorial(3) Figure uses iterative version to solve the factorial of the number n

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Trace of fact = factorial(3);

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Iterative Function factorial

Copyright ©2004 Pearson Addison-Wesley. All rights reserved EXAMPLE 10.5 The Fibonacci numbers are a sequence of numbers that have many varied uses. The Fibonacci sequence is defined as –Fibonacci 1 is1 –Fibonacci 2 is 1 –Fibonacci n is Fibonacci n-2 +Fibonacci n-1, for n>2

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Recursive Function fibonacci

Copyright ©2004 Pearson Addison-Wesley. All rights reserved EXAMPLE 10.6 Euclid’s algorithm for finding the gcd can be defined recursively –gcd(m,n) is n if n divides m evently –gcd(m,n) is gcd(n, remainder of m divided by n) otherwise

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Program Using Recursive Function gcd

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Program Using Recursive Function gcd (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved RECURSIVE FUNCTIONS WITH ARRAY AND STRING PARAMETERS CASE STUDY: FINDING CAPITAL LETTERS IN A STRING(1/4) Step 1: Problem –Form a string containing all the capital letters found in another string.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved CASE STUDY: FINDING CAPITAL LETTERS IN A STRING(2/4) Step 2: Analysis –Problem Input char *str –Problem Output char *caps

Copyright ©2004 Pearson Addison-Wesley. All rights reserved CASE STUDY: FINDING CAPITAL LETTERS IN A STRING(3/4) Step 3: Design –Algorithm 1. if str is the empty string 2. Store empty string in caps (a string with no letters certainly has no caps) else 3. if initial letter of str is a capital letter 4. Store in caps this letter and the capital letters from the rest of str else 5. Store in caps the capital letters from the rest of str

Copyright ©2004 Pearson Addison-Wesley. All rights reserved CASE STUDY: FINDING CAPITAL LETTERS IN A STRING(4/4) Step 4: Implementation (Figure10.15) Step 5: Testing (Figure 、 Figure 10.17)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Recursive Function to Extract Capital Letters from a String

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Trace of Call to Recursive Function find_caps

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Sequence of Events for Trace of Call to find_caps from printf Statements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved CASE STUDY RECURSIVE SELECTION SORT(1/4) Step 1: Problem –Sort an array in ascending order using a selection sort.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved CASE STUDY RECURSIVE SELECTION SORT(2/4) Step 2: Analysis (Figure 10.18)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Trace of Selection Sort

Copyright ©2004 Pearson Addison-Wesley. All rights reserved CASE STUDY RECURSIVE SELECTION SORT(3/4) Step 3: Design –Recursive algorithm for selection sort 1. if n is 1 2. The array is sorted. else 3. Place the largest array value in last array element 4. Sort the subarray which excludes the last array element (array[0]..array[n-2])

Copyright ©2004 Pearson Addison-Wesley. All rights reserved CASE STUDY RECURSIVE SELECTION SORT(4/4) Step 4: Implementation (Figure10.19) Step 5: Testing

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Recursive Selection Sort

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Recursive Selection Sort (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved PROBLEM SOLVING WITH RECURSION CASE STUDY OPERATIONS ON SETS(1/4) Step 1: Problem –Develop a group of functions to perform the E (is an element of), (is a subset of) and ∪ (union) operations on sets of characters. –Also develop functions to check that a certain set is valid (that is, that it contains no duplicate characters), to check for the empty set, and to print a set in standard set notation.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved CASE STUDY OPERATIONS ON SETS(2/4) Step 2: Analysis –Character strings provide a fairly natural representation of sets of characters. –Like sets, strings can be of varying sizes and can be empty. –If a character array that is to hold a set is declared to have one more than the number of characters in the universal set (to allow room for the null character), then set operations should never produce a string that will overflow the array.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved CASE STUDY OPERATIONS ON SETS(3/4) Step 3: Design –Algorithm for is_empty(set) 1. Is initial character ‘\0’ ? –Algorithm for is_element(ele, set) 1.if is_empty(set) 2. Answer is false else if initial character of set matches ele 3. Answer is true else 4. Answer depends on whether ele is in the rest of set

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Step 3: Design (cont’s) –Algorithm for is_set(set) 1.if is_empty(set) 2. Answer is true else if is_element(initial set character, rest of set) 3. Answer is false else 4. Answer depends on whether rest of set is valid set –Algorithm for is_subset(sub, set) 1.if is_empty(sub) 2. Answer is true else if initial character of sub is not an element of set 3. Answer is false else 4. Answer depends on whether rest of sub is a subset of set

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Step 3: Design (cont’s) –Algorithm for union of set1 and set2 1.if is_empty(set1) 2. Result is set2 else if initial character of sset1 is also an element of set2 3. Result is union of the rest of set1 with set2 else 4. Result includes initial character of set1 and the union of the rest of set1 with set2

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Step 3: Design (cont’s) –Algorithm for print_set(set) 1.Output a {. 2.if set is not empty, print elements seperated by commas. 3.Output a }. –Algorithm for print_with_commas(set) 1.If set has exactly one element 2. Print it else 3. Print initial element and a comma 4. print_with_commas the rest of set

Copyright ©2004 Pearson Addison-Wesley. All rights reserved CASE STUDY OPERATIONS ON SETS(4/4) Step 4: Implementation (Figure10.20) Step 5: Testing

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Recursive Set Operations on Sets Represented as Character Strings

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Recursive Set Operations on Sets Represented as Character Strings (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Recursive Set Operations on Sets Represented as Character Strings (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Recursive Set Operations on Sets Represented as Character Strings (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Recursive Set Operations on Sets Represented as Character Strings (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Recursive Set Operations on Sets Represented as Character Strings (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved A CLASSIC CASE STUDY IN RECURSION TOWERS OF HANOI (1/) Step 1: Problem –Move n disks from peg A to peg C using peg B as needed. –The following conditions apply 1. Only one disk at a time may be moved, and this disk must be the top disk on a peg 2. A larger disk can never be placed on top of a smaller disk

Copyright ©2004 Pearson Addison-Wesley. All rights reserved TOWERS OF HANOI (2/) Step 2: Analysis –Problem inputs int n char from_peg char to_peg char aux_peg –Problem output A list of individual disk moves

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Towers of Hanoi

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Towers of Hanoi After Steps 1 and 2

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Towers of Hanoi After Steps 1, 2, 3.1, and 3.2

Copyright ©2004 Pearson Addison-Wesley. All rights reserved TOWERS OF HANOI (3/) Step 3: Design –Algorithm 1.if n is 1 then 2. Move disk 1 from the from peg to the to peg else 3. Move n-1 disks from the from peg to the auxiliary peg using the to peg 4. Move disk n from the from peg to the to peg 5. Move n-1 disks from the auxiliary peg to the to peg using the from peg

Copyright ©2004 Pearson Addison-Wesley. All rights reserved TOWERS OF HANOI (4/4) Step 4: Implementation (Figure10.24) Step 5: Testing

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Recursive Function tower

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Trace of tower ('A', 'C', 'B', 3);

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Output Generated by tower ('A', 'C', 'B', 3);

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Comparison of Iterative and Recursive Functions Recursive –Requires more time and space because of extra function calls –Is much easier to read and understand –To researchers developing solutions to the complex problems that are at the frontiers of their research areas, the benefits gained from increased clarity far outweigh the extra cost in time and memory of running a recursive program

Copyright ©2004 Pearson Addison-Wesley. All rights reserved COMMON PROGRAMMING ERRORS A recursive function may not be terminate properly. –A run-time error message noting stack overflow or an access violation is an indicator that a recursive function is not terminating Be aware that it is critical that every path through a nonvoid function leads to a return statement The recopying of large arrays or other data structures can quickly consume all available memory Introduce a nonrecursive function to handle preliminaries and call the recursive function when there is error checking

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Chapter Review A recursive function either calls itself or initiates a sequence of function calls in which it may be called again Designing a recursive solution involves identifying simple cases that have straightforward solutions and then redefining more complex cases in terms of problems that are closer to simple cases Recursive functions depend on the fact that for each cal to a function, space is allocated on the stack for the function’s parameters and local variables

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Question?

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Programming Projects 1 Develop a program to count pixels (picture elements) belonging to an object in a photograph. The data are in a two-dimensional grid of cells, each of which may be empty (value 0) or filled (value 1). The filled cells that are connected form a blob (an object). Figure shows a grid with three blobs. Include in your program a function blob_check that takes as parameters the grid and the x-y coordinates of a cell and returns as its value the number of cells in the blob to which the indicated cell belongs.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Grid with Three Blobs