Unformatted text preview:

Lecture 4OutlineFormatting Tables 1Formatting Tables 2Course GoalsSoftware Life CycleRecursion 1Recursion 2Recursion 3In-class Exercise 1Recursion 4Recursion 5Iterative VersionIn-class Exercise 2Wednesday, January 19 CS 215 Fundamentals of Programming II - Lecture 4 1Lecture 4Log into LinuxTextbook is in the bookstoreReminder: Homework 2 program is due by Friday 4:30pm. The submission system is set up to accept submissions.Reminder: Sign up for Career Forum by Friday.Questions?Wednesday, January 19 CS 215 Fundamentals of Programming II - Lecture 4 2OutlineFormatting tablesCourse goalsRecursionWednesday, January 19 CS 215 Fundamentals of Programming II - Lecture 4 3Formatting TablesTo format data into tables, want to output data into a fixed-width field.In C++, done using I/O manipulators defined in <iomanip>. Manipulators are "output" to a stream using insertion operator (<<).setw(n) – output the next item in a field of size nleft, right – change justification within fields (default is right). This is in force until the next justification manipulator.Wednesday, January 19 CS 215 Fundamentals of Programming II - Lecture 4 4Formatting TablesExample:cout << left;cout << setw(4) << "n" << " " << setw(6) << "2^n" << endl;cout << setw(4) << "----" << " " << setw(6) << "------" << endl;cout << right;for (int i = 0; i < 16; i=i+4) cout << setw(4) << i << " " << setw(6) << pow(2,i) << endl;Output:n 2^n---- ------ 0 1 4 16 8 256 12 4096Wednesday, January 19 CS 215 Fundamentals of Programming II - Lecture 4 5Course GoalsContinue studying software engineering techniques with emphasis on implementing abstract data types using classes.Mastery of programming skills: files and other streams, recursion, classes, dynamic allocation, exception handling, templates.Introduction to algorithm analysis.Example: How to compute the sum from 1 to n?Wednesday, January 19 CS 215 Fundamentals of Programming II - Lecture 4 6Software Life CycleSpecification of the problem/taskDesign of a solutionImplementation (coding) of the solutionTesting and debuggingMaintenance and evolution of the systemObsolescenceWednesday, January 19 CS 215 Fundamentals of Programming II - Lecture 4 7What is Recursion?Recursion is a programming technique in which a problem is broken down into smaller versions of itself until the solution is obvious.General strategy for thinking about recursive solutions:Identify one or more base (anchor) cases, also called stopping conditions: Cases that are solved directly.Identify one or more recursive (inductive) steps: Solve the problem by dividing it into smaller version of itself. (Why must must new version be smaller?)Wednesday, January 19 CS 215 Fundamentals of Programming II - Lecture 4 8RecursionExample: Write a program that displays a non-negative integer to the screen so that its digits are stacked vertically. E.g. 1234 should display as:1234What is the simplest version of this problem?How do we identify it?Wednesday, January 19 CS 215 Fundamentals of Programming II - Lecture 4 9RecursionFor a number 10 or greater, how can we break up this problem so that we get a smaller version of the original problem?Consider different possible inputsHow can we get a single digit of a number?Look for ways that are easy to computeWednesday, January 19 CS 215 Fundamentals of Programming II - Lecture 4 10In-class Exercise, Part 1Copy file from csserver: /home/hwang/cs215/lecture04/inclass4.cppWrite the code for WriteVertical, the recursive function that displays the digits of a non-negative number vertically on the screen. This function receiv es the positive number it is to display. (There are no other parameters, nor does it return a result.)Wednesday, January 19 CS 215 Fundamentals of Programming II - Lecture 4 11How 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.Wednesday, January 19 CS 215 Fundamentals of Programming II - Lecture 4 12How Does Recursion Work?WriteVertical(1234)number: 1234number < 10 XWriteVertical(1234/10)cout << 1234%10 << endl;number: 123123 < 10 XWriteVertical(123/10)cout << 123%10 << endl;number: 12number < 10 XWriteVertical(12/10)cout << 12%10 << endlnumber: 1number < 10 cout << 1 << endlFirst outputSecond outputThird outputFourth outputWednesday, January 19 CS 215 Fundamentals of Programming II - Lecture 4 13Iterative 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; }}Wednesday, January 19 CS 215 Fundamentals of Programming II - Lecture 4 14In-class Exercise, Part 2Extend WriteVertical so that it handles negative numbers by displaying the negative sign above the digits.Example: WriteVertical(-361) would


View Full Document

UE CS 215 - Lecture 4

Documents in this Course
Lecture 5

Lecture 5

18 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 4
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 4 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 4 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?