CS1411 - 160 - Programming Principles I, Spring 2005
Lab assignment 4
Motivation
This weeks you will practice some more functions
Mandatory work
Your TA will check that you have done the following things (you may leave once their done) :
Pass by value vs. pass by reference
You are supposed to write functions to convert between celsius and fahrenheit and vice versa.
The formulas to do this are: celsius := (fahrenheit-32)*5/9 and fahrenheit = celsius*9/5+32. Please make sure you use loating point division!
To practice parameter passing, the celsius-to-fahrenheit should use pass-by-value and return the fahrenheit value as a return value. Its function header could look like this:
float cel2fah(float cel)
To practice pass-by-reference the fahrenheit to celsius function should use only one parameter and use that as input and output. Its header could look like this:
void fah2cel(float &temperature)
Test your work by providing a complete program with a main function that gives the user a chance to input a temperture and select a conversion. The main function could look like this (don't forget the includes and other important stuff!):
Warning: This code is only a suggestion. It may contain parse errors if you just cut'n'paste it!
int main() { float temperature; char what; cout << "Please enter a temperature: "; cin >> temperature; cout << "Is this in celsius (c) or fahrenheit (f)?"; cin >> what; if (what == 'c') cout << "This would be " << cel2fah(what) " fahrenheit" << endl; else { fah2cel(temperature); cout << "This would be " << temperature << " celsius" << endl; } return 0; }
Optional work
Some other things that you might try:
- Rewrite both function to use pass-by-value in the fahrenheit-to-celsius function and vice versa.
- Make the program repeatable with a do - while loop
- Instead of converting to the other one (celsius or fahrenheit), convert to Kelvin (see Wikipedia: Kelvin for formulas and information)
- Make the program all-purpose by entering a temperature in either Kelvin, Fahrenheit or Celsius and let the user convert it to either Kelvin, Fahrenheit or Celsius (Hint: Always convert to Kelvin inbetween is simpler than implementing 9 conversions)
How?
Please check the labs page on how to get started. There are instructions for visual studio (use version 6) and XCode
Please give the Macs a try. If you simply cannot work with them, go to PE 119, but let your TA know.
Sample programs
have moved to the examples page
Help!
For help: ask your TA! He is here to help you!
If the instructions are unclear or you have any other questions, please email me