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 4Log into LinuxTextbook is in the bookstoreReminder: 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 2OutlineFormatting tablesCourse goalsRecursionWednesday, January 19 CS 215 Fundamentals of Programming II - Lecture 4 3Formatting TablesTo 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 nleft, 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 TablesExample: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 GoalsContinue 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 CycleSpecification of the problem/taskDesign of a solutionImplementation (coding) of the solutionTesting and debuggingMaintenance and evolution of the systemObsolescenceWednesday, 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 8RecursionExample: 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:1234What is the simplest version of this problem?How do we identify it?Wednesday, January 19 CS 215 Fundamentals of Programming II - Lecture 4 9RecursionFor 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 inputsHow 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 1Copy file from csserver: /home/hwang/cs215/lecture04/inclass4.cppWrite 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 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.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 2Extend WriteVertical so that it handles negative numbers by displaying the negative sign above the digits.Example: WriteVertical(-361) would
View Full Document