9p / who / tweedy / 9C / 1
Exercise 1.5: Temperature Table (reverse)
Code
#include <u.h>
#include <libc.h>
/* print Celsius-Fahrenheit table for fahr = 0, 20, ..., 300;
floating-point version, with header, in reverse */
void main()
{
float fahr, celsius;
int lower, upper, step;
lower = 0; /* lower limit of temperature table */
upper = 300; /* upper limit */
step = 20; /* step size */
celsius = upper;
print(" C \t F\n---\t------\n");
while (celsius >= lower) {
fahr = celsius * (9.0/5.0) + 32;
print("%3.0f\t%6.1f\n", celsius, fahr);
celsius = celsius - step;
}
}
Output
$ 9c reverse.c; 9l reverse.o -o reverse
$ ./reverse
C F
--- ------
300 572.0
280 536.0
260 500.0
240 464.0
220 428.0
200 392.0
180 356.0
160 320.0
140 284.0
120 248.0
100 212.0
80 176.0
60 140.0
40 104.0
20 68.0
0 32.0
tweedy