### COSC 071: Computer Science I

Homework 2
Spring 2002

Written due in lecture, Tue, Feb 5th
E-mail due: Feb 1 @ 5 PM
5 points

This homework is designed to get you familiar with algorithms and with entering, compiling, and submitting C++ programs using either Visual C++ (Windows), CodeWarrior (Windows), or GNU g++ (UNIX).

1. Hoya Pizza sells a 9" square pizza with one topping for \$9.99, and Saxa Pizza sells an 10" round pizza with one topping for \$8.99. Which is the better buy?

2. As we've discussed, an algorithm is just a step-by-step procedure for accomplishing a task. The steps you took above constitute an algorithm, albeit a very specific algorithm. A limitation is that it only works for 9" square pizzas that cost \$9.99 and for 10" round pizzas that cost \$8.99. Instead of using the actual numbers provided, rewrite your previous ``algorithm'' using variables or symbols that represent the numbers. For example, instead of writing 9.99, write roundPizzaCost, and instead of writing 10, write roundPizzaSize.

For example, if we had a triangle pizza, we may write something like:

trianglePizzaCost = 7.99
trianglePizzaBase = 6
trianglePizzaHeight = 9
trianglePizzaArea = 0.5 x trianglePizzaBase x trianglePizzaHeight
trianglePriceSqIn = trianglePizzaCost / trianglePizzaArea

The advantage of doing this is that we've separated the procedure for calculating the price per square inch of a pizza from any set of specific numbers. To compute a new price per square inch, we simply reassign the variables that correspond to the cost and the size of the pizza, and apply the algorithm.

3. Assume that you are restricted to five statements:

1. input
2. output
3. calculate
4. if condition, then statement
5. if condition, then statement; otherwise, statement

For our triangle pizza example, we may write something like:

input trianglePizzaCost
input trianglePizzaBase
input trianglePizzaHeight
calculate trianglePizzaArea = 0.5 x trianglePizzaBase x trianglePizzaHeight
calculate trianglePriceSqIn = trianglePizzaCost / trianglePizzaArea
output trianglePriceSqIn

Express your algorithm for deciding which pizza is the better buy in terms of these statements.

4. Draw a flowchart for Question 3's solution.

5. On page 42, the authors present Euclid's algorithm for computing the greatest common divisor (GCD) of two integers. Use the algorithm to calculate the GCD of m = 17 and n = 3.

6. Draw a flowchart for Euclid's algorithm.

7. Using either CodeWarrior (Windows), Visual C++ (Windows), GNU g++ (UNIX), or a C++ compiler of your choosing, enter, compile, and run the C++ programs Program 2.1 (page 51), 2.2 (page 53), 2.6 (page 57), 2.8 (page 68), 2.9 (page 75), and Program 2.11 (page 94).

You can find CodeWarrior and Visual C++ on the UIS Windows machines that litter campus. We'll be submitting all projects this semester via e-mail using pine on the gusun system, so if you plan on using CodeWarrior or some other Windows or Mac C++ compiler, then you'll need to use DOS ftp or ws-FTP to transfer your source code from a Windows machine to gusun. There is a transcript of a DOS ftp session on the COSC-071 FAQ page. Also see: HOWTO transfer files to gusun with ws-FTP, HOWTO compile using Visual C++, and HOWTO compile using CodeWarrior

You can find the GNU g++ compiler on gusun, a UNIX machine. Use a telnet program to connect to these machines from the Internet or from a Windows machine on the campus network. Also see: HOWTO compile using g++

Note that students in previous classes had trouble with Microsoft's telnet client. Plus, it isn't secure, so I'd recommend using a secure telnet client, like SSH. There's also NetTerm, which isn't secure.

The editors on gusun are named ``vi'', which is a full-screen editor, and ``pico'', which is also a full-screen editor. The documentation for these editors is available on-line. For vi, type at the UNIX prompt ``man vi'' for UNIX's on-line manual pages. There are also several links to Web pages from the class's main page. Note that vedit is a version of vi that is intended for beginners. For pico, once inside the editor, you can type Control-G to get help. If you plan to use UNIX this semester, I'd highly recommend learning to use vi. pico is simple, but it is also simplistic, especially for editing C++ code.

8. Do exercises 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 2.12, 2.19, 2.20 on pages 98-100 in Cohoon and Davidson and submit these with the other written exercises in lecture.

Note: For problem 2.8, remove both include statements.

9. To learn how we'll be submitting projects, from gusun, e-mail an electronic version of Program 2.11 to your TA. Use your netid and the suffix ``.cc'' as the subject line. Regardless of what extension you use for your C++ program on your local machine, you must use the .cc extension in the subject line.

If you plan to use CodeWarrior or Visual C++ this semester, then you first must use ftp to transfer your source file from a Windows machine to gusun. A transcript of an ftp session appears on the COSC071 FAQ page. You can also use ws-FTP, which is a GUI version of ftp. See HOWTO transfer files to gusun with ws-FTP, HOWTO compile using Visual C++,

Once you use ftp to put your source file on gusun, use a telnet client to log in. This semester, you'll be submitting projects via e-mail using pine. Do not use another e-mail client. To run pine, type at the UNIX prompt:

```gusun% pine
```