UMBC CS 201, Fall 05
 UMBC CMSC 201 Fall '05 CSEE | 201 | 201 F'05 | lectures | news | help Search Notes:

# Celsius to Fahrenheit Table

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 <stdio.h> #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; }

## 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 Lessons

• 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.

CSEE | 201 | 201 F'05 | lectures | news | help

Tuesday, 13-Sep-2005 13:19:37 EDT