2

The following code outputs the second number as the maximum. If you need any other information please let me know.

#include <iostream>                                                     
#include <cstdlib>
using namespace std;
double *ComputeMaximum(const double *Max, const double *Min);

double *ComputeMaximum(const double *Max, const double *Min)
{
    return ((double *)((&Max > &Min) ? Max : Min));
}

int main(void)
{
    double *max;
    double Initial, Secondary;


    cout << "Enter the number followed by space then another number: ";
    cin >> Initial;
    cout << "\nIn-" << Initial;
    cin >> Secondary;
    cout << "\nSe-" << Secondary;
    //cout >> "Of " >> Inital >> "and " >> Secondary;
    //cout >> "the maximum is " >>
    max = ComputeMaximum((double*)&Initial,(double*)&Secondary);
    cout << "\nmax" << *max;
    return 0;
}

The next code outputs the first number as the maximum

#include <iostream>                                                     
#include <cstdlib>
using namespace std;
double *ComputeMaximum(const double *Max, const double *Min);

double *ComputeMaximum(const double *Max, const double *Min)
{
    return ((double *)((Max > Min) ? Max : Min));  // Here is the difference(& missing)
}

int main(void)
{
    double *max;
    double Initial, Secondary;


    cout << "Enter the number followed by space then another number: ";
    cin >> Initial;
    cout << "\nIn-" << Initial;
    cin >> Secondary;
    cout << "\nSe-" << Secondary;
    //cout >> "Of " >> Inital >> "and " >> Secondary;
    //cout >> "the maximum is " >>
    max = ComputeMaximum((double*)&Initial,(double*)&Secondary);
    cout << "\nmax" << *max;
    return 0;
}

What is being done wrong? I only need the maximum, not the second or first input. I got the answer. Thank YOu all.

Here it is:

#include <iostream>                                                     
#include <cstdlib>
using namespace std;
double *ComputeMaximum(const double *Max, const double *Min);

double *ComputeMaximum(const double *Max, const double *Min)
{
    return (double*)((*Max > *Min) ? Max : Min);
}

int main(void)
{
    double *max;
    double Initial, Secondary;


    cout << "Enter the number followed by space then another number: ";
    cin >> Initial;
    cout << "\nIn-" << Initial;
    cin >> Secondary;
    cout << "\nSe-" << Secondary;
    //cout >> "Of " >> Inital >> "and " >> Secondary;
    //cout >> "the maximum is " >>
    max = ComputeMaximum(&Initial, &Secondary);
    cout << "\nmax" << *max;
    return 0;
}
1
  • Based on your ComputeMaximum declaration, you don't need to write ComputeMaximum((double*)&Initial,(double*)&Secondary). Instead you can simply write: ComputeMaximum(&Initial, &Secondary).
    – besworland
    Oct 29, 2012 at 10:11

4 Answers 4

6
double *ComputeMaximum(const double *Max, const double *Min)
{
    return *Max > *Min ? Max : Min;
}

Note that I used *Max and *Min instead of &Max and &Min, i.e. dereferencing, not taking the address! Also note that you have a lot of unnecessary casts to double* for expressions that are already of the desired type. One exaple is your ComputeMaximum function body. Another example

max = ComputeMaximum((double*)&Initial,(double*)&Secondary);

Because Initial and Secondary are of type double, &Initial and &Secondary are of type double* so there is absolutely no need for the ugly unnecessary cast. Just use

max = ComputeMaximum(&Initial,&Secondary);

Similarly in other places.

I strongly recommend you to read a good book on C++.

6
  • What are some good C++ and C books? NVM just noticed the link Oct 29, 2012 at 10:20
  • @user1781382: I specifically posted a link :) I, for one, recommend S.Lippmann's C++ Primer, 4th or higher editions Oct 29, 2012 at 10:23
  • @user1781382: Also note that there is no book that is both good for C and C++. These are different languages with different paradigms, approaches, techniques, and philosophy. So first choose a language then learn it, without mixing the two Oct 29, 2012 at 10:25
  • I have to learn both for class, but I know the books I have aren't too helpful. Oct 29, 2012 at 10:29
  • @user1781382: If your class is called C/C++, then it's horrible, because it's like having a language class called Spanish/Italian. You can't find a book on Spanish/Italian. Oct 29, 2012 at 10:30
6

You're comparing addresses. The correct way would be:

double *ComputeMaximum(const double *Max, const double *Min)
{
    return *Max > *Min ? Max : Min;
}

Your versions:

(Max > Min)

compares the pointers themselves, and

(&Max > &Min)

compares the addresses of the pointers, which is, again, wrong.

Also, you don't need pointers, and note that you have std::max which you can use.

0
3

Why are you using pointer in the first place? It is not even needed.

The following is a better implementation:

double ComputeMaximum(double a, double b)
{
    return a > b ? a : b;
}

Or if you wish to do something like this:

ComputeMaximum(x,y) = 100; //modify the one which is maximum

then reference is what you need:

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

By the way, you may would like to see std::max (and std::min) from the Standard library.

0
0

Neither of them returns the actual maximum.

You are passing pointers to doubles. So the value of the variables Min,Max are an address that points to a double.

&Max > &Min ... This will compare the addresses of the variables Max,Min which are local to the function.

Max > Min ... This will compare the addresses that Min and Max point to (just address numbers).

*Max > *Min ... This will dereference the pointers, and compare the object they are pointing at. This is what you want to do...

return ((double *)((*Max > *Min) ? Max : Min));
2
  • Why do you need explicit double cast in front of ternary expression?
    – besworland
    Oct 29, 2012 at 10:14
  • you don't really, both return values are of the same type, and they will be inferred correctly by the compiler. just copy-pasting from the original... Oct 29, 2012 at 10:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.