UMBC CS 201, Spring 02
UMBC
CMSC 201 Spring '02
CSEE
|
201
|
201 S'02
|
lectures
|
news
|
help
c2ftable
The Task
Print a table for converting Celsius to Fahrenheit.
The Program
/*************************************************** * File: c2ftable.c * Author: D. Frey * Date: Aug 2, 1999 * SSN : 123-45-6789 * Section: 0101 * EMail: frey@cs.umbc.edu * * This program illustrates the use of functions * by generating a table of Celsius to Fahrenheit * conversions. ***************************************************/ #include
#define LOWER 0 /* Starting temp */ #define UPPER 100 /* Final temp */ #define STEP 5 /* step size */ /* Function prototypes */ double CelsiusToFahrenheit (double celsius); int main() { int celsius, fahrenheit; printf("Celsius to Fahrenheit table.\n"); printf(" C F\n"); for (celsius = LOWER; celsius <= UPPER; celsius += STEP) { fahrenheit = CelsiusToFahrenheit(celsius); printf("%3d %3d\n", celsius, fahrenheit); } return 0; } /************************************************** * Function: CelsiusToFahrenheit * Usage: fahren = CelsiusToFahrenheit (celsius); * * Inputs: degrees Celsius to be converted * Output: Returns the Fahrenheit equivalent * of the Celsius temperature provided ***************************************************/ double CelsiusToFahrenheit(double celsius) { return (9.0 / 5.0 * celsius + 32); }
The Sample Run
linux3[75] % a.out Celsius to Fahrenheit table. C F 0 32 5 41 10 50 15 59 20 68 25 77 30 86 35 95 40 104 45 113 50 122 55 131 60 140 65 149 70 158 75 167 80 176 85 185 90 194 95 203 100 212 linux3[76] %
The Lesson
Making the computation of celsius to fahrenheit a seperate function simplifies the organization of the program. The effect is not large in this case, but you will see more motivating examples soon.
CSEE
|
201
|
201 S'02
|
lectures
|
news
|
help
Thursday, 07-Feb-2002 14:14:54 EST