You are on page 1of 8

340 Chapter 6 • Pointers and Modular Programming

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);
}

c. What naming convention introduced in Section 6.2 is violated in the


formal parameter list of trouble?

6.6 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 demon-
strated 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 imple-
ments a set of functions for manipulating fractions.

CASE STUDY Collecting Area For Solar-Heated House

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

Structure Chart for Computing Solar Collecting Area Size

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

Determine coldest Get solar Get heating


Estimate
month, average insolation for requirement, Display
collecting
heating degree coldest month efficiency, floor results
area
days for it (function nth_item) space from user

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

Input file solar.txt


500 750 1100 1490 1900 2100 2050 1550 1200 900 500 500
344 Chapter 6 • Pointers and Modular Programming

FIGURE 6.13 Program to Approximate Solar Collecting Area Size

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

FIGURE 6.13 (continued)


41. if (next_hdd > heat_deg_days) {
42. heat_deg_days = next_hdd;
43. coldest_mon = ct;
44. }
45.
46. ++ct;
47. status = fscanf(hdd_file, "%d", &next_hdd);
48. }
49. fclose(hdd_file);
50.
51. /* Get corresponding average daily solar insolation from other file */
52. solar_file = fopen("solar.txt", "r");
53. solar_insol = nth_item(solar_file, coldest_mon);
54. fclose(solar_file);
55.
56. /* Get from user specifics of this house */
57. printf("What is the approximate heating requirement (BTU / ");
58. printf("degree day ft^2)\nof this type of construction?\n=> ");
59. scanf("%d", &heating_req);
60. printf("What percent of solar insolation will be converted ");
61. printf("to usable heat?\n=> ");
62. scanf("%d", &efficiency);
63. printf("What is the floor space (ft^2)?\n=> ");
64. scanf("%lf", &floor_space);
65.
66. /* Project collecting area needed */
67. heat_loss = heating_req * floor_space * heat_deg_days;
68. energy_resrc = efficiency * 0.01 * solar_insol *
69. days_in_month(coldest_mon);
70. collect_area = (int)(heat_loss / energy_resrc + 0.5);
71.
72. /* Display results */
73. printf("To replace heat loss of %.0f BTU in the ", heat_loss);
74. printf("coldest month (month %d)\nwith available ", coldest_mon);
75. printf("solar insolation of %d BTU / ft^2 / day,", solar_insol);
76. printf(" and an\nefficiency of %d percent,", efficiency);
77. printf(" use a solar collecting area of %d", collect_area);
78. printf(" ft^2.\n");
79.
80. return (0);
81. } (continued)
346 Chapter 6 • Pointers and Modular Programming

FIGURE 6.13 (continued)


82.
83. /*
84. * Given a month number (1 = January, 2 = February, …,
85. * 12 = December ), return the number of days in the month
86. * (nonleap year).
87. * Pre: 1 <= monthNumber <= 12
88. */
89. int days_in_month( int month_number )
90. {
91.
92. int ans;
93.
94. switch (month_number) {
95. case 2: ans = 28; /* February */
96. break;
97.
98. case 4: /* April */
99. case 6: /* June */
100. case 9: /* September */
101. case 11: ans = 30; /* November */
102. break;
103.
104. default: ans = 31;
105. }
106.
107. return ans;
108. }
109.
110. /*
111. * Finds and returns the nth integer in a file.
112. * Pre: data_file accesses a file of at least n integers (n >= 1).
113. */
114. int nth_item(FILE *data_file, int n)
115. {
116. int i, item;
117.
118. for (i = 1; i <= n; ++i)
119. fscanf(data_file, "%d", &item);
120.

(continued)
6.6 • Problem Solving Illustrated 347

FIGURE 6.13 (continued)


121. return item;
122. }

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.

Functions for Common Fractions


In our next case study, we manipulate numeric data of a type not provided as one
of C’s base types. In order to do this, we must write our own functions to perform
operations on common fractions that we take for granted when using types int
and double.

CASE STUDY Arithmetic with Common Fractions

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.

You might also like