Professional Documents
Culture Documents
trouble(&x, &y);
printf("x = %d, y = %d\n", x, y);
return (0);
}
void
double_trouble(int *p, int y)
{
int x;
x = 10;
*p = 2 * x - y;
}
void
trouble(int *x, int *y)
{
double_trouble(x, 7);
double_trouble(y, *x);
}
PROBLEM
An architect needs a program that can estimate the appropriate size for the col-
lecting 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
6.6 • Problem Solving Illustrated 341
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
heat loss
A 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*/
342 Chapter 6 • Pointers and Modular Programming
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 ft2 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.
FIGURE 6.12
Estimate solar
collecting area
size
heating_req
efficiency
solar_file floor_space
hdd_file heat_deg_days
coldest_mon
solar_insol collect_area
coldest_mon solar_insol heating_req coldest_mon collect_area
heat_deg_days efficiency
floor_space
coldest_mon num_days
days_in_month
6.6 • Problem Solving Illustrated 343
STEP 1 REFINEMENT
We will introduce three new variables to use in our refinement—a counter, ct,
to keep track of our position in the heating degree days file, an integer variable to
record file status, and an integer variable next_hdd to hold each heating degree
days value in turn.
Additional Program Variables
ct /* position in file */
status /* input status */
next_hdd /* one heating degree days value */
1.1 Scan first value from heating degree days file into heat_deg_days, and initial-
ize coldest_mon to 1.
1.2 Initialize ct to 2.
1.3 Scan a value from the file into next_hdd, saving status.
1.4 As long as no faulty data or not at end of file, repeat
1.5 if next_hdd is greater than heat_deg_days
1.6 Copy next_hdd into heat_deg_days.
1.7 Copy ct into coldest_mon.
1.8 Increment ct.
1.9 Scan a value from the file into next_hdd, saving status.
STEP 4 REFINEMENT
4.1 Calculate heat_loss as the product of heating_req, floor_space, and
heat_deg_days.
4.2 Calculate energy_resrc as the product of efficiency (converted to hun-
dredths), solar_insol, and the number of days in the coldest month.
4.3 Calculate collect_area as heat_loss divided by energy_resrc. Round
result to nearest whole square foot.
We will develop a separate function for finding the number of days in a month, a
value needed in step 4.2 (see Fig. 6.12).
Functions
Functions nth_item and days_in_month are quite simple, so we will show only
their implementation. Figure 6.13 is an implementation of the entire program for
approximating the necessary size of a solar collecting area for solar heating a certain
structure in a given geographic area.
Input file hdd.txt
995 900 750 400 180 20 10 10 60 290 610 1051
1. /*
2. * Estimate necessary solar collecting area size for a particular type of
3. * construction in a given location.
4. */
5. #include <stdio.h>
6.
7. int days_in_month(int);
8. int nth_item(FILE *, int);
9.
10. int main(void)
11. {
12. int heat_deg_days, /* average for coldest month */
13. solar_insol, /* average daily solar radiation per
14. ft^2 for coldest month */
15. coldest_mon, /* coldest month: number in range 1..12 */
16. heating_req, /* BTU / degree day ft^2 requirement for
17. given type of construction */
18. efficiency, /* % of solar insolation converted to
19. usable heat */
20. collect_area, /* ft^2 needed to provide heat for
21. coldest month */
22. ct, /* position in file */
23. status, /* file status variable */
24. next_hdd; /* one heating degree days value */
25. double floor_space, /* ft^2 */
26. heat_loss, /* BTUs lost in coldest month */
27. energy_resrc; /* BTUs heat obtained from 1 ft^2
28. collecting area in coldest month */
29. FILE *hdd_file; /* average heating degree days for each
30. of 12 months */
31. FILE *solar_file; /* average solar insolation for each of
32. 12 months */
33.
34. /* Get average heating degree days for coldest month from file */
35. hdd_file = fopen("hdd.txt", "r");
36. fscanf(hdd_file, "%d", &heat_deg_days);
37. coldest_mon = 1;
38. ct = 2;
39. status = fscanf(hdd_file, "%d", &next_hdd);
40. while (status == 1) {
(continued)
6.6 • Problem Solving Illustrated 345
(continued)
6.6 • Problem Solving Illustrated 347
Sample Run
What is the approximate heating requirement (BTU / degree day ft^2)
of this type of construction?
=> 9
What percent of solar insolation will be converted to usable heat?
=> 60
What is the floor space (ft^2)?
=> 1200
To replace heat loss of 11350800 BTU in the coldest month (month 12)
with available solar insolation of 500 BTU / ft^2 / day, and an
efficiency of 60 percent, use a solar collecting area of 1221 ft^2.
PROBLEM
You are working problems in which you must display your results as integer ratios;
therefore, you need to be able to perform computations with common fractions and get
results that are common fractions in reduced form. You want to write a program that
will allow you to add, subtract, multiply, and divide several pairs of common fractions.
ANALYSIS
Because the problem specifies that results are to be in reduced form, we will need
to include a fraction-reducing function in addition to the computational functions. If
we break the problem into small enough chunks, there should be an opportunity to
reuse code by calling the same function multiple times. The in-depth analysis of the
problem is actually distributed through the development of these functions.