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

3.1 Problem Solving: Building Programs from Existing Information

Programmers seldom start off with a blank slate (or empty screen) when they develop a program. Often some--or all--of the solution can be developed from information that already exists or from the solution to another problem.

Carefully following the software development method generates important system documentation before you even begin to code a program. Such documentation, consisting of a description of a problem's data requirements (developed during the analysis phase) and its solution algorithm (developed during the design phase), summarizes your intentions and thought processes.

You can use this documentation as a starting point in coding your program. For example, you can begin by copying the problem data requirements into the program declaration section, then editing those lines to conform to the Ada syntax for constant and variable declarations, thereby completing the declaration section of the program. This approach is especially helpful if the documentation was created with a word processor and is in a file that you can edit.

To develop the program body, first use the initial algorithm and its refinements as program comments. The comments describe each algorithm step and provide program documentation that guides your Ada code. After the comments are in place in the program body, you can begin to write the Ada statements. Place the code for an unrefined step directly under that step. For a refined step, either edit the refinement to convert it from English to Ada or just replace it with Ada code. We illustrate the entire process in the next two case studies.


Case Study: Finding Area and Circumference of a Circle

PROBLEM SPECIFICATION

Read in the radius of a circle and compute and print its area and circumference.

ANALYSIS

Clearly, the problem input is the circle radius. Two ouputs are requested: thecircle area and circumference. These variables should be type Float because the inputs and outputs may contain fractional parts. The geometric relationships between a circle's radius and its area and circumference are listed next, along with the data requirements.

Data Requirements and Formulas

Problem Constant

Pi : CONSTANT Float := 3.14159; 

Problem Inputs

Radius : NonNegFloat --radius of a circle

Problem Outputs

Area : NonNegFloat --area of a circle
Circum : NonNegFloat --circumference of a circle

Relevant Formulas

area of a circle = pi * radius2

circumference of a circle = 2 * pi * radius

DESIGN

Once you know the problem inputs and outputs, list the steps necessary to solve the problem. Pay close attention to the order of the steps.

Initial Algorithm

1. Read the circle radius.

2. Find the area.

3. Find the circumference.

4. Print the area and circumference.

Algorithm Refinements

Next, refine any steps that do not have an obvious solution (steps 2 and 3).

Step 2 Refinement

2.1. Assign Pi * Radius * Radius to Area.

Step 3 Refinement

3.1. Assign 2 * Pi * Radius to Circum.

TEST PLAN

The special cases needing to be tested are zero radius and negative radius. A zero radius should give zero area and circumference; a negative radius should be an error.

IMPLEMENTATION

Program 3.1 is the Ada program so far. The program body consists of the initial algorithm with its refinements. This outline contains the "framework" consisting of PROCEDURE, BEGIN, and END, some declarations, and just comments in the program body. Including the statement

    NULL;
just after the BEGIN in fact makes the program syntactically correct Ada even though it has no other statements. It can be compiled just to check whether the basic framework and declarations are correct.

Program 3.1
Framework for Area and Circumference

PROCEDURE Area_And_Circum_Frame IS
------------------------------------------------------------------------
--| Finds and displays the area and circumference of a circle  
--| 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;
 
   Radius        : NonNegFloat;  -- input  - radius of a circle
   Area          : NonNegFloat;  -- output - area of a circle
   Circumference : NonNegFloat;  -- output - circumference of a circle
 
BEGIN -- Area_And_Circum_Frame
  NULL;
 
  -- 1. Read the circle radius   
 
  -- 2. Find the area   
  -- 2.1 Assign Pi * Radius ** 2 to Area
 
  -- 3. Find the circumference   
  -- 3.1 Assign 2.0 * Pi * Radius to Circumference
 
  -- 4. Display the Area and Circumference 
 
END Area_And_Circum_Frame;

SYNTAX DISPLAY
Null Statement

Form:
NULL;

Example:
PROCEDURE SmallestAdaProcedure IS
BEGIN
	NULL;
END SmallestAdaProcedure;
Interpretation:
The null statement is used to indicate an "empty" sequence of statements. NULL is sometimes used to satisfy a syntax rule requiring a sequence of statements, even when the sequence is (intentionally) empty.

To write the final program, we must

Program 3.2 is the final program.

Program 3.2
Area and Circumference

WITH Ada.Text_IO;
WITH Ada.Float_Text_IO;
PROCEDURE Area_And_Circum IS
------------------------------------------------------------------------
--| Finds and displays the area and circumference of a circle  
--| 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;
 
   Radius        : NonNegFloat;  -- input  - radius of a circle
   Area          : NonNegFloat;  -- output - area of a circle
   Circumference : NonNegFloat;  -- output - circumference of a circle
 
BEGIN -- Area_And_Circum   
 
  -- Read the circle radius   
  Ada.Text_IO.Put (Item => "Enter radius > ");                                
  Ada.Float_Text_IO.Get (Item => Radius);  
 
  -- Find the area   
  Area := Pi * Radius ** 2;
 
  -- Find the circumference   
  Circumference := 2.0 * Pi * Radius;
 
  -- Display the Area and Circumference 
  Ada.Text_IO.Put (Item => "The area is ");
  Ada.Float_Text_IO.Put (Item => Area, Fore => 1, Aft => 2, Exp => 0);
  Ada.Text_IO.New_Line;
  Ada.Text_IO.Put (Item => "The circumference is ");
  Ada.Float_Text_IO.Put (Item => Circumference, Fore => 1, Aft => 2, Exp => 0);
  Ada.Text_IO.New_Line;
 
END Area_And_Circum;
Sample Run
Enter radius > 5.0
The area is 78.54
The circumference is 31.42
TESTING

The sample run shows a good test of the solution because it is relatively easy to compute the area and circumference by hand for a radius value of 5.0. The radius squared is 25.0, so the value of the area appears correct. The circumference should be 10 times [[onesuperior]], which is also an easy number to compute by hand. We leave the other tests in the test plan for you to complete.


Exercises for Section 3.1

Self-Check

  1. Describe the problem inputs and outputs and algorithm for computing anemployee's gross salary given the hours worked and hourly rate.
  2. Describe the problem inputs and outputs and algorithm for the following problem: Read in a pair of numbers and determine the sum and average of the two numbers.

Programming

  1. Write a program for Self-Check question 2.


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

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