Problem Solving Illustrated
In
this section, we examine two programming problems that illustrate many of the
concepts
discussed in this chapter. The top-down design process
will be demonstrated
in
solving each programming problem. Each program will be implemented
in
a stepwise manner, starting with a list of major algorithm steps and continuing
to
add
detail through refinement until the program and its function subprogram can be
written.
The first problem uses files and file pointers. The second problem implements
a set of functions
for manipulating fractions.
PROBLEM
An
architect needs a program that can estimate the appropriate size for the
collecting
area
of a solar-heated house. Determining collecting area size requires
consideration
of several factors, including the average number of heating degree
days
for the coldest month of a year (the product of the average difference between
inside
and outside temperatures and the number of days in the month), the heating requirement
per square foot of floor space, the floor space, and the efficiency of
the
collection method. The program will have access to two data files. File hdd.txt
contains
numbers representing the average heating degree days in the construction
location for
each of 12 months. File solar.txt contains the average solar insolation
(rate in
BTU/day at which solar radiation falls on one square foot of a given location)
for each
month. The first entry in each file represents data for January, the second,
data for February, and so on.
ANALYSIS
The
formula for approximating the desired collecting area ( A ) is
A _ heat loss
energy resource
In
turn, heat loss is computed as the product of the heating requirement, the floor
space,
and the heating degree days. We compute the necessary energy resource by
multiplying
the efficiency of the collection method by the average solar insolation
per
day and the number of days.
In
all of our previous programs, data for program inputs have come from the
same
source—either the keyboard or a file. In this program we will use three input
sources:
the two data files and the keyboard. We can now identify the problem’s
data
requirements and develop an algorithm.
DATA REQUIREMENTS
Problem Inputs
Average
heating degree days file
Average
solar insolation file
heat_deg_days /* average heating degree days for coldest month */
coldest_mon /* coldest month (number 1 .. 12) */
solar_insol /* average daily solar insolation (BTU/ft^2)for
coldest month */
heating_req /* BTU/degree day ft^2 for planned type construction*/
efficiency /* % of solar insolation converted to usable heat */
floor_space /* square feet */
Program Variables
energy_resrc /* usable solar energy available in coldest month
(BTUs obtained from 1 ft^2 of collecting area) */
Problem Outputs
heat_loss /* BTUs of heat lost by structure in coldest month */
collect_area
/* approximate size (ft^2) of collecting area needed*/
DESIGN
Initial Algorithm
1.
Determine the coldest month and the average heating degree days for this
month.
2.
Find the average daily solar insolation per ft 2
for the coldest month.
3.
Get from the user the other problem inputs: heating_req , efficiency , and
floor_space .
4.
Estimate the collecting area needed.
5.
Display results.
As
shown in the structure chart ( Fig. 6.12 ), we will design step 2 as a separate
function.
Function nth_item will find the value in file solar.txt that corresponds to
the
coldest month. Steps 3 and 5 are quite straightforward, so only steps 1 and 4
call
for refinement
here.
Geen opmerkings nie:
Plaas 'n opmerking