Lecture 4OutlineIn-class Exercise, Last ClassRecursion 4Recursion 5Iterative VersionIn-class Exercise 2Generating FractalsLine Segment FractalWhy Study Fractals?Function SpecifcationExampleFunction DesignImplementation 1Implementation 2Implementation 3Implementation 4In-class Exercise 3Friday, September 3 CS 215 Fundamentals of Programming II - Lecture 5 1Lecture 5Homework 3 is posted. It is a written assignment due next Wednesday.Log into LinuxDoes everyone have the in-class exercise from last class? (inclass4.cpp)File must have your name in the header comment.Friday, September 3 CS 215 Fundamentals of Programming II - Lecture 5 2OutlineRecursionGenerating FractalsFriday, September 3 CS 215 Fundamentals of Programming II - Lecture 5 3In-class Exercise, Last Classvoid WriteVertical(int number){ if (number < 10) // base case, one digit cout << number << endl; else { // recursive step // write all but last digit WriteVertical(number/10); // then write the last digit cout << (number%10) << endl; }}Friday, September 3 CS 215 Fundamentals of Programming II - Lecture 5 4How Does Recursion Work?Trace functions as followsFor every function call, draw an arrow from the call to a new function box.Inside the box, write down the parameter names and their correspondence to the actual arguments.Also write down any local variable names.Trace the function code starting with the first line. Substitute parameter values.When the function returns, draw an arrow back (with the result, if any) to the call and continue tracing the code after the call.Friday, September 3 CS 215 Fundamentals of Programming II - Lecture 5 5How Does Recursion Work?WriteVertical(1234)number: 12341234 < 10 XWriteVertical(1234/10)cout << 1234%10 << endl;number: 123123 < 10 XWriteVertical(123/10)cout << 123%10 << endl;number: 1212 < 10 XWriteVertical(12/10)cout << 12%10 << endlnumber: 11 < 10 cout << 1 << endlFirst outputSecond outputThird outputFourth outputFriday, September 3 CS 215 Fundamentals of Programming II - Lecture 5 6Iterative Version// From Savitch, _Problem Solving with C++_void WriteVertical(int n) { int tensInN = 1; int leftEndPiece = n; // tensInN is a power of ten that has the same number // of digits as n. while (leftEndPiece > 9) { leftEndPiece = leftEndPiece/10; tensInN = tensInN * 10; } // Successively print out digits from left to right for (int powerOf10 = tensInN; powerOf10 > 0; powerOf10 = powerOf10 / 10) { cout << (n / powerOf10) << endl; n = n % powerOf10; }}Friday, September 3 CS 215 Fundamentals of Programming II - Lecture 5 7In-class Exercise, Part 2Extend WriteVertical so that it handles negative numbers by displaying the negative sign above the digits.Example: WriteVertical(-361) would display-361When you are done, print out your program and hand it in.Friday, September 3 CS 215 Fundamentals of Programming II - Lecture 5 8Example: Generating FractalsFractals are one of the tools in graphics used to generate natural looking scenes of mountains, clouds, trees, etc.A simple fractal example is as follows:Start with a straight line segmentCompute the midpoint of the lineMove the midpoint up or down a random amountRepeat with each new line segmentFriday, September 3 CS 215 Fundamentals of Programming II - Lecture 5 9Line Segment Fractala) Initial Line Segmentb) Midpoint moved up a random distancec) Two midpoints moved up and down a random distanced) Four midpoints moved up and down a random distanceFriday, September 3 CS 215 Fundamentals of Programming II - Lecture 5 10Why Study Fractals?After many steps, the line looks jagged.But if we magnify the line, we would see that the magnified portion looks remarkably similar to the overall shape of the line.This appears a lot in nature, which is why fractals are used to generate scenery.Friday, September 3 CS 215 Fundamentals of Programming II - Lecture 5 11Function SpecificationWrite a function RandomFractal that computes and draws a random line fractal.Receives (book uses "input"): leftHeight and rightHeight, the height of the left and right endpoints measured from a fixed base line at the original line segment's left endpoint.width, the horizontal width from left to right endpointsepsilon, the width at which the process stops and the line is drawn; usually very smallFriday, September 3 CS 215 Fundamentals of Programming II - Lecture 5 12ExampleOriginal function call might be (distance in inches):RandomFractal(0.0, 1.5, 2, 0.6)After 2 steps, will stop and might have fractal aboveFunction should generate and display height of right endpoints0.00.5"1.00.70.7-0.3baselineFriday, September 3 CS 215 Fundamentals of Programming II - Lecture 5 13Function DesignBase case is easy1. If width is less than or equal to epsilon 1.1 Display rightHeight.Recursive step follows the generation algorithm1. Compute random height for the midpoint a. Compute current midHeight = (rightHeight+leftHeight)/2 b. Add random number to midHeight2. Generate random fractals for the new line segments a. RandomFractal (leftHeight, midHeight, width/2, epsilon) b. RandomFractal (midHeight, rightHeight, width/2, epsilon)Friday, September 3 CS 215 Fundamentals of Programming II - Lecture 5 14ImplementationCopy files /home/hwang/cs215/lecture05/*.* from csserverFiles useful.h and useful.cpp are from the textbook Appendix I. File fractal.cpp is a slightly modified version of textbook code provided for Figure 9.4.Function prototype:void RandomFractal( double leftHeight, double rightHeight, double width, double epsilon);Friday, September 3 CS 215 Fundamentals of Programming II - Lecture 5 15ImplementationOnce again, base case is easy:if (width <= epsilon) display (rightHeight);The display function prints out a line of output with a vertical bar in the middle. If the argument n is positive, then approximately n stars will be displayed to the right of the bar. If n is negative, then approximately n starts will be displayed to the left of the bar. See useful.cpp for more information.Friday, September 3 CS 215 Fundamentals of Programming II - Lecture 5 16ImplementationSince the recursion generates the left segments before the right segments, the left end of the fractal will be at the top of the screen and the fractal is generated sideways.The recursive step also is straightforward. To compute the
View Full Document