I am learning c++ and I come across problems in this program. What I'm trying to do is to write a function computing max of 2 parameters x and y, each of type “reference to const double”. The function returns type “reference to double”. Return the entire conditional expression, type casting it as (double &) to override the “const" variable.
So I write this function in a file named "fmax.cpp"
double& fmax(const double &x, const double &y)
{
return (x > y) ? x : y; //Need type casting here?
}
I try to call the funciton in another file main.cpp
include <iostream>
using namespace std;
double& fmax(const double &x, const double &y);
int main()
{
const double &x; //should I put &x or x here?
const double &y; //should I put &y or y here?
cout << "enter 2 values" << '\n';
cin >> x >> y; //should I put & or no & here?
//Am I calling the function correctly?
cout << fmax(double &x, double &y) << '\n';
return 0;
}
Please advise how to write the program correctly.