* I meant pointer parameters

This function that has two parameters, both of type reference and return reference to pointer.
Do I need to fix anything?
I have been stumped for days and dunno what I should fix

here is my code:

#include <iostream>
using namespace std;

double *ComputeMaximum( const double *num1a, const double *num2a);

int main() 
{
    double num1;
    double num2;
    cout << "Please enter two numbers: ";
    cin >> num1 >> num2;
    cout << "The greatest value is: " << ComputeMaximum(num1, num2) << " and its pointer is: " << *ComputeMaximum(num1, num2);
    return 0;
}

double *ComputeMaximum( const double *num1a, const double *num2a)
{
    return (double *)(num1a > num2a ? num1a : num2a);
}

Line 16 compares the values of the two pointers, not the values of the doubles to which they point.

Also, if your argument is const double *, that means you're promising not to change the value to which the pointer points. If you now return double *, you're violating that promise.

Fix these two problems and your ComputeMaximum function looks like this:

const double *ComputeMaximum(const double *num1a, const double *num2a)
{
    return *num1a > *num2a? num1a: num2a;
}

Because you are expecting pointers rather than references, you need to pass the addresses of the numbers in line 12. Also, you have swapped value and address:

cout << "The greatest value is: " << *ComputeMaximum(&num1, &num2) << " and its pointer is: " << ComputeMaximum(&num1, &num2);

And, of course, you need to change line 4 to match the function:

const double *ComputeMaximum(const double *num1a, const double *num2a);

the output for my references was this:


Please enter two numbers: 1 4
The greatest value is: 4 and its address is: 0x7fff5fbff7b0


when I did what you edited, (thanks so much by the way), my output was:

Please enter two numbers: 1 4
The greatest value is: 4 and its pointer is: 0x7fff5fbff7b0

Does that sound about right?

You could do this in an even cleaner fashion by using references. If you use const references, you can pass in values that have not been saved in a variable:

#include <iostream>
#include <vector>

using namespace std;

const double& max( const double& a, const double& b )
{
    return a > b ? a : b;
}

int main()
{
    cout << max( 5.0, 7.0 ) << endl;
    double a = 13.0;
    double b = 11.0;
    double c = max( a, b );
    cout << c << endl;
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.