# Fall 2005

## Clay Shields

### Homework 1 - Thinking About Algorithms

Due in class September 14th, 2005.
This homework is intended to get you thinking about algorithms. Recall that an algorithm is just an ordered series of steps that can be used to accomplish a particular task.
1. It is late at night, and you are hungry. You want to order a pizza. You have two flyers advertising cheap (but oh so delicious) pizzas. One is from Hoya Pizza, which sells a 9 inch square pizza for \$9.99. One is from Saxa Pizza, which sells a 10 inch round pizza for \$8.99. Which pizza is the better buy?
2. The steps you took to find the best pizza buy above constitutes an algorithm, albeit a very specific algorithm. A limitation is that it only works for 9 inch square pizzas that cost \$9.99 and for 10 inch round pizzas that cost \$8.99. What we would like to do is generalize this algorithm to work with different sizes and costs of pizzas.

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 four actions: INPUT, CALCULATE, DECISION, and OUTPUT. Express your algorithm for deciding which pizza is the better buy in terms of these actions. Do not invent or add your own actions, use only these four, and every step must use one of these actions.

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
4. There are many different ways of expressing algorithms. A graphical method that shows the order that things execute is called a flowchart. While not an endorsement of their product, the first two pages give a simple example of what a flowchart looks like. Draw a flowchart of the algorithm you developed for the last problem.
5. In this class, we will need to be able to read and follow algorithms as well as developing and writing our own. Do problems 29 through 32 in Chapter 1 of the text, and include the answers in your homework submission.