Lecture 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 1 Outline Formatting tables Course goals Recursion Wednesday, January 19 CS 215 Fundamentals of Programming II - Lecture 4 2 Formatting 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 3 Formatting 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 4096 Wednesday, January 19 CS 215 Fundamentals of Programming II - Lecture 4 4 Course 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 5 Software 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 Obsolescence Wednesday, January 19 CS 215 Fundamentals of Programming II - Lecture 4 6 What 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 7 Recursion Example: Write a program that displays a nonnegative integer to the screen so that its digits are stacked vertically. E.g. 1234 should display as: 1 2 3 4 What is the simplest version of this problem? How do we identify it? Wednesday, January 19 CS 215 Fundamentals of Programming II - Lecture 4 8 Recursion 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 compute Wednesday, January 19 CS 215 Fundamentals of Programming II - Lecture 4 9 In-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 receives 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 10 How 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 11 How Does Recursion Work? WriteVertical(1234) Fourth output number: 1234 number < 10 X WriteVertical(1234/10) cout << 1234%10 << endl; number: 123 123 < 10 X WriteVertical(123/10) cout << 123%10 << endl; Second output number: 12 number < 10 X WriteVertical(12/10) cout << 12%10 << endl number: 1 number < 10 cout << 1 << endl Third output Wednesday, January 19 CS 215 Fundamentals of Programming II - Lecture 4 First output 12 Iterative 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 13 In-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 ­ 3 6 1 Wednesday, January 19 CS 215 Fundamentals of Programming II - Lecture 4 14