Chapter 1-2, exercises 1-3 and 1-4
This commit is contained in:
parent
f69b32679a
commit
3a4ab22746
4 changed files with 56 additions and 0 deletions
BIN
1-2/temperature3
Executable file
BIN
1-2/temperature3
Executable file
Binary file not shown.
28
1-2/temperature3.c
Normal file
28
1-2/temperature3.c
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
/*
|
||||||
|
* Print a table of Fahrenheit temperatures and their Celsius equivalents (floating point)
|
||||||
|
* From K&R 2nd ed., 1-2
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main (void) {
|
||||||
|
float fahr, celsius;
|
||||||
|
float lower, upper, step;
|
||||||
|
|
||||||
|
lower = 0; // Lower limit of temperature scale
|
||||||
|
upper = 300; // Upper limit of temperature scale
|
||||||
|
step = 20; // Step size
|
||||||
|
|
||||||
|
fahr = lower;
|
||||||
|
|
||||||
|
printf("FAHRENHEIT TO CELSIUS TABLE\n");
|
||||||
|
|
||||||
|
while (fahr <= upper) {
|
||||||
|
celsius = (5.0/9.0) * (fahr-32.0);
|
||||||
|
printf("%3.0f %6.5f\n", fahr, celsius);
|
||||||
|
fahr = fahr + step;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
BIN
1-2/temperature4
Executable file
BIN
1-2/temperature4
Executable file
Binary file not shown.
28
1-2/temperature4.c
Normal file
28
1-2/temperature4.c
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
/*
|
||||||
|
K&R C 2nd Edition, exercise 1-4
|
||||||
|
Convert Celsius temperatures to Fahrenheit (floating point)
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
float fahr, celsius;
|
||||||
|
float upper, lower, step;
|
||||||
|
|
||||||
|
upper = 200;
|
||||||
|
lower = 0;
|
||||||
|
step = 10;
|
||||||
|
|
||||||
|
celsius = lower;
|
||||||
|
|
||||||
|
printf("Celsius to Fahrenheit conversion table\n\n");
|
||||||
|
|
||||||
|
while (celsius <= upper) {
|
||||||
|
fahr = celsius * (9.0/5.0) + 32.0;
|
||||||
|
printf("%6.0f %6.2f\n", celsius, fahr);
|
||||||
|
celsius = celsius + step;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue