Question:
C++ function program please help?
Edward
2010-05-03 08:40:40 UTC
Im trying to write a function named celsius that accepts a Fahrenheit temperature as an argument. The function should return the temperature, converted to celsius. Also i have to demonstrate the function by calling it in a loop that displays a table from 0 through 20 and the celsius equivalents.

THIS IS WHAT I HAVE SO FAR:
#include
#include
using namespace std;


double celsius(double);

int main()
{

cout << setprecision(12) << fixed << showpoint;

cout << "Farenheit" << setw(12) << right << "celsius";
cout << "\n------------------------------------------\n";

for (double f = 0; f <= 20; f++)
{
cout << setw(10) << right << f
<< setw(12) << right << celsius(f)
<< endl;
}
return 0;
}

double celsius(double f)
{
double c = (5.0/9.0) * (f - 32);

return c;
}

IT TOTALLY DISPLAYS THE WRONG ANSWERS!!! I GET LIKE A BUNCH OF DECIMAL NUMBERS WITH ALL NEGATIVE SYMBOLS? I THINK A PARENTHESIS IS WRONG SOMEWHERE BUT IDK!?!?!

PLEASE HELP!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Three answers:
coachabower
2010-05-03 08:52:30 UTC
Without testing i don't know for sure, but try changing the param of celsius to an int.

Run your for loop with f as an int.

You can still make the return type of celsius a double, just make the input param an int.



Try that.



This is assuming the 0-20 you need to display the conversion for only includes the integer values of 0-20.

If this function is eventually going to accept a Fahrenheit input from a user that can be a double this wont work.

In that case try making your for loop an int still, but cast it as a double when you pass it to the function.
?
2016-04-12 08:06:30 UTC
What is a Function? A function is a block of code that has a name and it has a property that it is reusable i.e. it can be executed from as many different points in a C Program as required. Function groups a number of program statements into a unit and gives it a name. This unit can be invoked from other parts of a program. A computer program cannot handle all the tasks by it self. Instead its requests other program like entities – called functions in C – to get its tasks done. A function is a self contained block of statements that perform a coherent task of same kind The name of the function is unique in a C Program and is Global. It neams that a function can be accessed from any location with in a C Program. We pass information to the function called arguments specified when the function is called. And the function either returns some value to the point it was called from or returns nothing. We can divide a long C program into small blocks which can perform a certain task. A function is a self contained block of statements that perform a coherent task of same kind. Structure of a Function There are two main parts of the function. The function header and the function body. int sum(int x, int y) { int ans = 0; //holds the answer that will be returned ans = x + y; //calculate the sum return ans //return the answer } Function Header In the first line of the above code int sum(int x, int y) It has three main parts The name of the function i.e. sum The parameters of the function enclosed in paranthesis Return value type i.e. int Function Body What ever is written with in { } in the above example is the body of the function. Function Prototypes The prototype of a function provides the basic information about a function which tells the compiler that the function is used correctly or not. It contains the same information as the function header contains. The prototype of the function in the above example would be like int sum (int x, int y); The only difference between the header and the prototype is the semicolon ; there must the a semicolon at the end of the prototype. Happy Learning, Good Luck :)
cja
2010-05-03 09:11:56 UTC
You're getting the correct answers, but the display doesn't look very good. Try this:



#include

#include



using namespace std;



double celsius(double);



int main(int argc, char *argv[]) {



    cout << setprecision(12) << fixed << showpoint;



    cout << " Farenheit" << setw(11) << right << "Celsius";

    cout << endl << "-------------------------" << endl;



    for (double f = 0; f <= 20; f++) {

        cout << setw(10) << setprecision(2) << right << f

                  << setw(12) << setprecision(2) << right << celsius(f)

                  << endl;

    }

    return 0;

}



double celsius(double f) {

    double c = (5.0/9.0) * (f - 32);



    return c;

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...