Previous | Next | Table of Contents | Index | Program List | Copyright

3.2 Problem Solving: Extending a Problem Solution

Another way programmers reuse existing information is by noting that the solution of one problem is often the basis for the solution to another problem. For example, we can easily solve the next problem by building on the solution to the previous problem.


Case Study: Unit Price of a Pizza

PROBLEM SPECIFICATION

You and your college roommates frequently order a late-night pizza snack. There are many pizzerias in the area that deliver to dormitories. Because you are on a tight budget, you would like to know which pizza is the best value.

ANALYSIS

To find which pizza is the best value, we must be able to do a meaningfulcomparison of pizza costs. One way to do this is to compute the unit price of each pizza. The unit price of an item is obtained by dividing the total price of that item by a measure of its quantity. A good measure of quantity is the pizza weight, but pizzas are not sold by weight--they are sold by size (diameter), measured in inches. Consequently, the best that we can do is to use some meaningful measure of quantity based on the pizza diameter. One such measure is the pizza area. So, for our purposes we will define the unit price of a pizza as its price divided by its area.

The data requirements below list the pizza size and price as problem inputs. Although the problem statement does not ask us to display the pizza area, we are listing it as a problem output because the pizza area will give us some idea of how many friends we can invite to share our pizza. The radius (one-half of the diameter) is listed as a program variable because we need it to compute the pizza area, but it is not a problem input or output.

Data Requirements

Problem Constant

Pi : CONSTANT Float := 3.14159; 

Problem Inputs

Size : NonNegFloat -- diameter of a pizza
Price : NonNegFloat -- price of a pizza

Problem Outputs

Area : NonNegFloat -- area of a pizza
UnitPrice : NonNegFloat -- unit price of a pizza

Relevant Formulas

area of a circle = Pi * radius2

radius of a circle = diameter/2

unit price = price/area

DESIGN

We mentioned earlier that we are basing the problem solution on the solution to the Case Study in Section 3.1 (finding the area and circumference of a circle). The initial algorithm is similar to the one shown earlier. The step that computes the circle circumference (step 3) has been replaced with one that computes the pizza unit price.

Initial Algorithm

1. Read in the pizza diameter and price.

2. Compute the pizza area.

3. Compute the pizza unit price.

4. Display the unit price and area. The refinement of step 2 shows that we must compute the pizza radius before we can compute its area.

Step 2 Refinement

2.1 Assign Diameter / 2 to Radius.

2.2. Assign Pi * Radius * Radius to Area.

Step 3 Refinement

3.1. Assign Price / Area to UnitPrice.

TEST PLAN

To test this program, run it with a few different pizza sizes. You can verify that the program is working correctly by multiplying the unit price and area. This product should equal the price of the pizza.

IMPLEMENTATION

Program3.3 shows the framework for the Ada program. We will write this program the same way as before: by editing the data requirements to develop the program declaration part and by using the initial algorithm with refinements as a starting point for the program body.

In Program 3.3, instead of defining our own constant Pi, we are using the constant Pi provided by an Ada standard library, Ada.Numerics ( Appendix F). There, Pi is given to 50 decimal places.[1] Note that to use this library, we just write the usual context clause

    WITH Ada.Numerics;
and then get the value of Pi as Ada.Numerics.Pi.

Program 3.3
Framework for Pizzeria

WITH Ada.Numerics;
PROCEDURE Pizzeria_Frame IS
------------------------------------------------------------------------
--| Computes and displays the unit price of a pizza
--| Author: Michael B. Feldman, The George Washington University 
--| Last Modified: July 1995                                     
------------------------------------------------------------------------

  SUBTYPE NonNegFloat IS Float RANGE 0.0 .. Float'Last;
  
  -- Pi : CONSTANT NonNegFloat := 3.14159;
  -- unnecessary; better to get this from the standard library!
  -- we can just refer to Ada.Numerics.Pi


  Diameter  : NonNegFloat;  -- input  - diameter of a pizza
  Price     : NonNegFloat;  -- input  - price of a pizza
  UnitPrice : NonNegFloat;  -- output - unit price of a pizza
  Area      : NonNegFloat;  -- output - area of a pizza
  Radius    : NonNegFloat;  -- radius of a pizza

BEGIN -- Pizzeria_Frame
  NULL;

  -- 1. Read in the pizza diameter and price

  -- 2. Compute the pizza area
  -- 2.1 Assign Diameter/2 to Radius
  -- 2.2 Assign Pi * Radius ** 2 to Area

  -- 3. Compute the pizza unit price
  -- 3.1 Assign Price / Area to UnitPrice

  -- 4. Display the unit price and area

END Pizzeria_Frame;
Program 3.4 gives the final program.

Program 3.4
Unit Price of a PIzza

WITH Ada.Text_IO;
WITH Ada.Float_Text_IO;
WITH Ada.Numerics;
PROCEDURE Pizzeria IS
------------------------------------------------------------------------
--| Computes and displays the unit price of a pizza
--| Author: Michael B. Feldman, The George Washington University 
--| Last Modified: July 1995                                     
------------------------------------------------------------------------
 
  SUBTYPE NonNegFloat IS Float RANGE 0.0 .. Float'Last;

  -- Pi : CONSTANT NonNegFloat := 3.14159;
  -- unnecessary; better to get this from the standard library!
 
  Diameter  : NonNegFloat;  -- input  - diameter of a pizza
  Price     : NonNegFloat;  -- input  - price of a pizza
  UnitPrice : NonNegFloat;  -- output - unit price of a pizza
  Area      : NonNegFloat;  -- output - area of a pizza 
  Radius    : NonNegFloat;  -- radius of a pizza
 
BEGIN -- Pizzeria
 
  -- Read in the pizza diameter and price
  Ada.Text_IO.Put (Item => "Size of pizza in inches > ");
  Ada.Float_Text_IO.Get (Item => Diameter);
  Ada.Text_IO.Put (Item => "Price of pizza $");
  Ada.Float_Text_IO.Get (Item => Price);  
 
  -- Compute the pizza area   
  Radius := Diameter/2.0;
  Area := Ada.Numerics.Pi * Radius ** 2;
 
  -- Compute the pizza unit price
  UnitPrice := Price / Area;
 
  -- Display the unit price and area
  Ada.Text_IO.New_Line;
  Ada.Text_IO.Put (Item => "The pizza unit price is $");
  Ada.Float_Text_IO.Put (Item => UnitPrice, Fore=>1, Aft=>2, Exp=>0);
  Ada.Text_IO.New_Line;
  Ada.Text_IO.Put (Item => "The pizza area is ");
  Ada.Float_Text_IO.Put (Item => Area, Fore => 1, Aft => 2, Exp => 0);
  Ada.Text_IO.Put (Item => " square inches.");
  Ada.Text_IO.New_Line;
 
END Pizzeria;
Sample Run
Size of pizza in inches > 10
Price of pizza $8.50

The pizza unit price is $0.11
The pizza area is 78.54 square inches.
TESTING

The sample run gives one test. You can supply others.


PROGRAM STYLE
Using Comments

Comments make a program more readable by describing the purpose of the program and by describing the use of each identifier. For example, the comment in the declaration

Radius: NonNegFloat; -- program input - radius of a circle

describes the use of the variable Radius.

You should place comments within the program body to describe the purpose of each section of the program. The stepwise refinement method we use in this book uses comments in the program framework for each step of the algorithm and its refinements. Some of these comments are turned into program statements as these are written; others remain as program documentation.

You may wish to add other comments to a program to make it easier for yourself and others to understand. Make sure a comment within the program body adds useful descriptive information about what the step does rather than simply restate the step in English. For example, the comment

-- Find the area of the circle
Area := Pi * Radius * Radius;
is more descriptive than, and therefore preferable to,

-- Multiply the Radius by itelf and Pi
Area := Pi * Radius * Radius;

Begin each program with a header section, sometimes called a block comment or banner comment, that consist of a series of comments specifying:

If you write the program for a class assignment, you should also list the class identification and your instructor's name. Your instructor may also require other kinds of comments in your program.

A final word on comments: If a program has too few comments, the reader may have difficulty understanding the program. On the other hand, if there are too many comments, finding the program text among the comments will be difficult. Writing effective comments--knowing just how much to write--is a skill that must be practiced.


[1] Ada.Numerics is new in Ada 95. In Ada 83, no standard numerics or math library was specified, so compiler implementers generally provided their own. In Ada 95 this has become part of the standard.


Previous | Next | Table of Contents | Index | Program List | Copyright

Copyright © 1996 by Addison-Wesley Publishing Company, Inc.