Celsius and Fahrenheit Temperatures

Greetings! This is my first post. I have been reading this website for help for homework for a little now and am stumped on a few things.

I am a little lost overall on functions and parameters and how they are useful to my program. I have read the article here on the site regarding functions but it just isn't sticking for some reason.

I also have a homework question. My assignment is described in my book as follows:

(Celsius and Fahrenheit Temperatures) Implement the following integer functions:
a) Function celsius returns the Celsius equivalent of Fahrenheit temperature.
b) Function fahrenheit returns the Fahrenheit equivalent of a Celsius temperature.
C) Use these functions to write a program that prints charts showing the Fahrenheit equivalents of all Celsius temperatures from 0 to 100 degrees, and the Celsius equivalents of all Fahrenheit temperatures from 32 to 212 degrees. Print the outputs in a neat tabular format that minimizes the number of lines and output while remaining readable.

This is what I have thus far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <iomanip>
using namespace std;

double celsius();
double fahrenheit();




int main()
{
    

    
    
    
    
    return 0;
}
Functions are useful because the help break your code up into smaller pieces, which improves readability and reusability. Lemme elaborate on that.

In cases where you have to perform the same action several times at different points in a program (that a loop is insufficient for), then a function can be invaluable.
In other cases, a loop can still help make your program easier to read (which is a benefit for you and whoever might read your code) by taking blocks of code which serve a specific purpose out of main and putting them into their own function.
Finally, in cases where someone else might want to use your code in the form of a library (assuming they have your permission), then giving them functions to work with that can be easily integrated into their program is one of the ways you can let them use it.

Now to respond to your assignment.

I think those functions were meant to take in fahrenheit and celsius values in parameter and return celsius and fahrenheit values respectively. You've got the "return" bit down fine, but they still need to take parameters.

Declaring that a function is going to take parameters is as simple as making a list of comma-separated variable declarations. Example:

some_obscure_type some_function(some_obscure_type a, some_obscure_type b, int generations, bool ignoreDiscrepencies);

Actually calling a function is similarly simple:

1
2
3
4
5
6
7
8
some_obscure_type rabbit, wolf;
bool doLongTerm = false;
//INSERT CODE THAT MANIPULATES THE ABOVE VARIABLES HERE.
some_obscure_type chimera;
if(doLongTerm)
    chimera = some_function(rabbit, wolf, 1, false); //FUNCTION CALL!
else
    chimera = some_function(rabbit, wolf, 70, true); //FUNCTION CALL! 


Did this help at all?

-Albatross
You need to have functions which convert some value to other value, so they should not only return "double" but also take "double" as parameter:

1
2
3
4
5
6
7
double fahrenheit(double celsDegrees) {
    return .... ;
}

double celsius(double fahrDegrees) {
    return .... ;
}


First of them should divide input by 5 and multiply by 9, then add 32.

Second should do the reversed.

http://codeabbey.com/index/task_view/fahrenheit-celsius
Here are example of values and place to check your answers.

I also recommend you to read the famous book by Kernighan and Ritchie - it will guide you through basics of programming language - and it also have this task as one of exercises.
Sadly, no.

I am still pretty lost with this assignment and some of the terminology.
Instead of trying to think of the whole thing at once, break it up into parts. For example, the first function:
a) Function celsius returns the Celsius equivalent of Fahrenheit temperature.

You would need to send the Fahrenheit value, as a parameter, do the math to convert it, and return the Celsius value.

Just get that function working, then the other function will be a snap. From there its mostly only a matter of formatting the output.
What do you think of this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <iomanip>
using namespace std;

double toFahrenheit(double Celsius);

int main()
{
    cout << "Celsius";
    cout << setw(20);
    cout << "Fahrenheit" << endl;
    
    for (double i = 0; i <= 100; i++)
    {
        cout << i << endl;
        cout << setw(20);
        cout << toFahrenheit(i) << endl;
    }
    
    return 0;
}

double toFahrenheit(double Celsius)
{
    return (double) ((Celsius * (9.0/5.0)) +32);
}
Have you read through the tutorial page(s) on functions?
http://www.cplusplus.com/doc/tutorial/functions/

The first example uses a function called addition (), and shows how to write the code for a simple function. If you can follow that (at least get a feel for the way it should look), then you should be well on your way to writing your own functions.
I would keep main simple until I got the functions working, something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int Main()
{
	double Far;
	double Cel;

	cout << "Enter temperature to covert to Fahrenheit: ";
	cin >> Cel;

	Far = toFahrenheit(Cel);

	cout << Far;

	return 0;
}

But otherwise, what Chervil said.
Topic archived. No new replies allowed.