Print a table for converting Celsius to Fahrenheit.
The Program
/***************************************************
* File: cel2fahren.c
* Author: S. Evans
* Date: Sep 13, 2005
* Section: 201H 0101
* EMail: bogar@cs.umbc.edu
*
* This program illustrates the use of symbolic constants,
* for loop control and the use of a general function for
* generating a table of Celsius to Fahrenheit conversions.
***************************************************/
#include
#define LOWER 0
#define UPPER 100
#define STEP 5
/* 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. This function's
* formal parameter and return type are of type double to
* make the function reusable without modification for any
* programs requiring the conversion of temperatures.
*
* Inputs: degrees Celsius to be converted
* Output: Returns the Fahrenheit equivalent
* of the Celsius temperature provided
***************************************************/
double CelsiusToFahrenheit(double celsius)
{
double fahrenheit;
fahrenheit = 9.0 / 5 * celsius + 32;
return fahrenheit;
}
Making the computation of celsius to fahrenheit a
separate function simplifies the organization of the
program. The effect is not large in this case, but you
will see more motivating examples soon.
Using symbolic constants for
loop control makes the program easily modifiable.
Using type double for the function's formal parameter
and return type makes this function reusable by any
program that needs to make this kind of conversion.