solving problems recursively print words entered but
play

Solving Problems Recursively Print words entered, but backwards Can - PowerPoint PPT Presentation

Solving Problems Recursively Print words entered, but backwards Can use a vector, store all the words and print in reverse order Recursion is an indispensable tool in a programmers toolkit The vector is probably the best approach,


  1. Solving Problems Recursively Print words entered, but backwards ● Can use a vector, store all the words and print in reverse order ● Recursion is an indispensable tool in a programmer’s toolkit ➤ The vector is probably the best approach, but recursion works too ➤ Allows many complex problems to be solved simply void PrintReversed() { ➤ Elegance and understanding in code often leads to better string word; programs: easier to modify, extend, verify if (cin >> word) // reading succeeded? ➤ Sometimes recursion isn’t appropriate, when it’s bad it can { PrintReversed(); // print the rest reversed be very bad---every tool requires knowledge and cout << word << endl; // then print the word experience in how to use it } } int main() ● The basic idea is to get help solving a problem from { coworkers (clones) who work and act like you do PrintReversed(); } ➤ Ask clone to solve a simpler but similar problem ● The function PrintReversed reads a word, prints the word ➤ Use clone’s result to put together your answer only after the clones finish printing in reverse order ● Need both concepts: call on the clone and use the result ➤ Each clone has its own version of the code, its own word variable A Computer Science Tapestry 10.1 A Computer Science Tapestry 10.2 Exponentiation Faster exponentiation ● Computing x n means multiplying n numbers (or does it?) How many recursive calls are made to computer 2 1024 ? ● ➤ What’s the easiest value of n to compute x n ? ➤ How many multiplies on each call? Is this better? ➤ If you want to multiply only once, what can you ask a clone? double Power(double x, int n) // post: returns x^n double Power(double x, int n) { // post: returns x^n if (n == 0) { return 1.0; { } if (n == 0) double semi = Power(x, n/2); { if (n % 2 == 0) return 1.0; { return semi*semi; } } return x * Power(x, n-1); return x * semi * semi; } } ● What about an iterative version? What about an iterative version of this function? ● A Computer Science Tapestry 10.3 A Computer Science Tapestry 10.4

  2. Blob Counting: Recursion at Work Counting blobs, the first slide prompt> blobs ● Blob counting is similar to what’s called Flood Fill, the enter row col size 10 50 method used to fill in an outline with a color (use the paint- # pixels on: between 1 and 500: 200 can in many drawing programs to fill in) +--------------------------------------------------+ | * * * * * * *** * **** * * | ➤ Possible to do this iteratively, but hard to get right | * * *** ** ** * * * * * * *| | * *** * * *** * * * * * * * * **| ➤ Simple recursive solution | * ** ** * ** * * * *** * * | | * * ***** *** * * ** ** * | |* * * * * * ** * *** * *** *| ● Suppose a slide is viewed under a microscope |* * *** * ** * * * * * ** | |* * ** * * * * *** ** * | ➤ Count images on the slide, or blobs in a gel, or … | **** * * ** **** * *** * * **| |** * * * ** **** ** * * ** *| ➤ Erase noise and make the blobs more visible +--------------------------------------------------+ ● To write the program we’ll use a class CharBitMap which ● How many blobs are there? Blobs are connected horizontally and vertically, suppose a minimum of 10 cells in a blob represents images using characters ➤ What if blob size changes? ➤ The program blobs.cpp and class Blobs are essential too A Computer Science Tapestry 10.5 A Computer Science Tapestry 10.6 Identifying Larger Blobs Identifying smaller blobs blob size (0 to exit) between 0 and 50: 10 blob size (0 to exit) between 0 and 50: 5 ....1............2................................ .................1................................ ....1.1........222................................ ...............111................................ ....111.....333.2.......................4......... ................1................................. ...........33..22......................444....5... ...............11................................. .........33333.222............6.......44.....55... ...............111............2................... ................2.............6.....444.....555... ................1.............2................... ...............222...77.......6.......4........... ...............111...33.......2................... ...8.............2...7........666.66.............. .................1...3........222.22.............. .8888...........22..7777........666............... 88..8...............77.7777....................... ................11..3333........222............... ....................33.3333....................... # blobs = 8 # blobs = 3 ● The class Blobs makes a copy of the CharBitMap and then ● What might be a problem if there are more than nine blobs? ➤ Issues in looking at code: how do language features get in counts blobs in the copy, by erasing noisy data (essentially) the way of understanding the code? ➤ In identifying blobs, too-small blobs are counted, then ➤ How can we track blobs, e.g., find the largest blob? uncounted by erasing them A Computer Science Tapestry 10.7 A Computer Science Tapestry 10.8

  3. Issues that arise in studying code Recursive helper functions ● Client programs use Blobs::FindBlobs to find blobs of a ● What does static mean, values defined in Blobs? given size in a CharBitMap object ➤ Class-wide values rather than stored once per object ➤ All Blob variables would share PIXEL_OFF , unlike ➤ This is a recursive function, private data is often myBlobCount which is different in every object needed/used in recursive member function parameters ➤ Use a helper function, not accessible to client code, use ➤ When is static useful? recursion to implement member function ● What is the class tmatrix? ● To find a blob, look at every pixel, if a pixel is part of a blob, ➤ Two-dimensional vector, use a[0][1] instead of a[0] identify the entire blob by sending out recursive clones/scouts ➤ First index is the row, second index is the column ➤ Each clone reports back the number of pixels it counts ➤ Each clone “colors” the blob with an identifying mark ● We’ll study these concepts in more depth, a minimal ➤ The mark is used to avoid duplicate (unending) work understanding is needed to work on blobs.cpp A Computer Science Tapestry 10.9 A Computer Science Tapestry 10.10 Conceptual Details of BlobFill Saving blobs ● In current version of Blobs::FindBlobs the blobs are ● Once a blob pixel is found, four recursive clones are “sent out” looking horizontally and vertically, reporting pixel count counted ➤ How are pixel counts processed by clone-sender? ➤ What changes if we want to store the blobs that are found? ➤ What if all the clones ultimately report a blob that’s small? ➤ How can clients access the found blobs? ➤ What is a blob, does it have state? Behavior? ● In checking horizontal/vertical neighbors what happens if ➤ What happens when a new minimal blob size is specified? there aren’t four neighbors? Is this a potential problem? ➤ Who checks for valid pixel coordinates, or pixel color? ● Why are the Blob class declaration, member function ➤ Two options: don’t make the call, don’t process the call implementations, and main function in one file? ➤ Advantages in using? blobs.h, blobs.cpp, doblobs.cpp? ● Non-recursive member function takes care of looking for blobsign, then filling/counting/unfilling blobs ➤ How does Makefile or IDE take care of managing multiple ➤ How is unfill/uncount managed? file projects/programs? A Computer Science Tapestry 10.11 A Computer Science Tapestry 10.12

Recommend


More recommend