Print a table for converting Celsius to Fahrenheit.
The Program
/***************************************************
* File: c2ftable.c
* Author: D. Frey
* Date: Aug 2, 1999
* Section: 0101
* EMail: frey@cs.umbc.edu
*
* This program illustrates the use of symbolic constants,
* for loops and 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;
/* prints table headings */
printf("Celsius to Fahrenheit table.\n");
printf(" C F\n");
/* generates a table of values from LOWER to UPPER by
STEP where each cycle calculates the value for and
prints one line of the table */
for (celsius = LOWER; celsius <= UPPER; celsius += STEP)
{
fahrenheit = CelsiusToFahrenheit(celsius);
printf("%3d %3d\n", celsius, fahrenheit);
}
return 0;
}
/**************************************************
* Function: CelsiusToFahrenheit
* Usage: fahren = CelsiusToFahrenheit (celsius);
*
* This function takes in a Celsius temperature and
* returns its Fahrenheit equivalent.
*
* 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);
}
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.