DOC PREVIEW
UE CS 215 - Lecture 5

This preview shows page 1-2-3-4-5-6 out of 18 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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 5Homework 3 is posted. It is a written assignment due next Wednesday.Log into LinuxDoes 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 2OutlineRecursionGenerating 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 followsFor 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 2Extend WriteVertical so that it handles negative numbers by displaying the negative sign above the digits.Example: WriteVertical(-361) would display-361When you are done, print out your program and hand it in.Friday, September 3 CS 215 Fundamentals of Programming II - Lecture 5 8Example: Generating FractalsFractals 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 segmentCompute the midpoint of the lineMove the midpoint up or down a random amountRepeat 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 SpecificationWrite 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 endpointsepsilon, 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 12ExampleOriginal function call might be (distance in inches):RandomFractal(0.0, 1.5, 2, 0.6)After 2 steps, will stop and might have fractal aboveFunction 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 DesignBase 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 14ImplementationCopy files /home/hwang/cs215/lecture05/*.* from csserverFiles 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 15ImplementationOnce 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 16ImplementationSince 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

UE CS 215 - Lecture 5

Documents in this Course
Lecture 4

Lecture 4

14 pages

Lecture 6

Lecture 6

17 pages

Lecture 7

Lecture 7

28 pages

Lecture 1

Lecture 1

16 pages

Lecture 5

Lecture 5

15 pages

Lecture 7

Lecture 7

28 pages

Load more
Download Lecture 5
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 5 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 5 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?