Exercise 1-15 (Temperature conversion function)
Chapter_1 Exercise_1-14-2 power | args Exercise_1-16 |
Exercise 1-15 K&R, p. 27
Exercise 1-15. Rewrite the temperature conversion program of Section 1.2 to use a function for conversion.
Note: We used the for()_variant from Section 1.3, which is similar to the programs in Section 1.2.
fahrf.c K&R, Sec. 1.2-1.3 download
#include <stdio.h> // for printf()
float ftoc (int); // Fahrenheit to Celsius
// print Fahrenheit-Celsius table
int main()
{
int fahr;
for (fahr = 0; fahr <= 300; fahr += 20)
{printf("%3d %6.1f\n", fahr, ftoc(fahr));}
return 0;
}
float ftoc (int f) // Fahrenheit to Celsius
{
return (5.0/9.0)*(f-32);
}
/*
gcc fahrf.c -o fahrf
./fahrf
0 -17.8
20 -6.7
40 4.4
60 15.6
80 26.7
100 37.8
120 48.9
140 60.0
160 71.1
180 82.2
200 93.3
220 104.4
240 115.6
260 126.7
280 137.8
300 148.9
*/
Chapter_1 Exercise_1-14-2 power | BACK_TO_TOP | args Exercise_1-16 |
Comments
Post a Comment