views:

292

answers:

3

Hello again,

I have this question which I don't understand and need help here.

1) I have this function double covertD(double, int), which takes in two arguements and returns a double. It will have to convert between Fahrenheit and Celsius and vice versa. This conversion depends on the passed in integer value.

Test case 1

If the passed in values are 212.0 and 1, the function converts the temperature 212.0 to Celsius.

Test case 2

If the passed in values are 100.0 and 2, the function converts the temperature 100.0 to Fahrenheit.

How do I write the heading and function body?

I dont understand the whole qns.

The passed in values in the test cases 212 and 100 are the temperautres, how about the 1 and 2? what are the passed in values actually - user inputs?

Thanks in advance!

from jeff's advice, i tried the following:

#include <iostream>

double degC = ((degF - 32) * 5/9);
double degF = ((degC * 9/5) + 32);

double covertDegrees(double value, int type) {
   double temp_out = 0.0;
   if (type == 1) {
      // Fahrenheit to Celsius.
      degC = value;
      temp_out = degF// Formula here.
   } else if (type == 2) {
      // Celsius to Fahrenheit.
      degF = value;
      temp_out = degC// Formula here.
   } else {
      // Error.
       cout << "error" << endl;
   }
   return temp_out;
}

im very noob at c++. is this correct?

+9  A: 

Here's a link to the formulas.

You basically want to do something like:

double covertD(double value, int type) {
   double temp_out = 0.0;
   if (type == 1) {
      // Fahrenheit to Celsius.
      temp_out = // Formula here.
   } else if (type == 2) {
      // Celsius to Fahrenheit.
      temp_out = // Formula here.
   } else {
      // Error.
   }
   return temp_out;
}
jeffamaphone
+1 For not doing all the work (even though it's most of it)
Toon Van Acker
A: 

let's just say i dont understand the whole qns.

the passed in values in the test cases 212 and 100 are the temperautres, how about the 1 and 2? what are the passed in values actually - user inputs?

RealiX
Yes, they're user values. Typically this function would be the backing logic behind some sort of UI. For example, you ask the user the input value (the temp) and whether it should be converted to C or F. Then you call this function.
jeffamaphone
The 1 indicates that the other input is in farenheit and should be converted to celsius. The 2 indicates the opposite conversion should be done. It shouldn't matter to your function where the values came from - user input, or read from a file for instance.
jon hanson
Please edit your question instead of adding another answer.
derobert
I edited this into the question. In the future, keep in mind SO isn't a forum, it's more like a wiki. Editing is cheap. If you have new information edit it into the question, don't put it in an answer.
jalf
sorry guys, i didnt realise there's "add comments" . i've editted my first post with my try out, please advice if its correct. thanks!!
RealiX
A: 

The basic idea seems to be that the first parameter is the input temperature, and the second parameter is a "flag" which tells the program what to do.

So if the second parameter is 1, convert from Fahrenheit to Celsius; if it is two, convert from Celsius to Fahrenheit. If the name of the function is, say, convertTemperature, you could either do

convertTemperature(212.0, 1) which should return 100.0 or convertTemperature(100.0, 2) which should return 212.0

There is no conceptual difference between the two parameters; they both come from the "user" where by "user" I mean the person who will use this function -- i.e., a programmer who could be you or could be someone else at some other time. (This is distinct from the user of that program -- you don't have to make the user type in "1" or "2"; this could, eventually, live deep inside a program with a complicated graphical interface, for example.

For the purposes of writing this program, first, ask yourself what would be good names for the two parameters. If you can answer that, you can write a full header for your function and then use the formulae that you either know or can look up to do the calculation.

Update: to comment on your code attempt above.

C++ programs aren't like, say, spreadsheets (or mathematics). The line

double degC = ((degF - 32) * 5/9);

is executed only when the computer gets to it. So it assigns the value of the expression on the right hand side to the variable on the left, once and only once. If you want a function to do this, you could write

double degC(double degF) {
    return ((degF - 32.0) * 5.0/9.0);
}

which you can then use as 'temp_out=degC(value)'.

Note that in this case these formulae are sufficiently simple that you don't really need to make functions out of them; you could just use the equations inline in your code.

Andrew Jaffe