COSC 235: Programming and Problem Solving
February 13, 2009

Joseph D. Sloan
sloanjd @ wofford.edu


Program Design

Code by imitation

Code by careful design—Carpet calculation example

1. Understand the problem

We want to calculate the cost of carpet for a room. We'll need the cost of the carpet and the size of the room and we'll get the total cost

2. Work once by hand (if feasible)

Let the room be 10 feet by 12 feet and the (lavish) carpet $10 per square foot. The size of the room is 10 x 12 or 120 square feet. The cost of the carpet is 120 x $10 or $1200.

3. Identify inputs

The inputs are length and width of the room and the cost of the carpet. Notice we have named two variable in the process.

4. Identify outputs

The output is the totalCost of the carpet

5. Identify steps in calculation

Calculate the area of the room: area = length * width
Calculate the totalCost of the carpet: totalCost = area * cost

6. Write code (decide if interactive or not)

Interactive & non-interactive

7. Test Code

Test on boundary and with known values first. Designing tests can be difficult!

This page was created by Joe Sloan.
It was last modified around: 013 February 2009
Send mail to: sloanjd@wofford.edu