Reputation: 157
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;
}
Upvotes: 2
Views: 251
Reputation: 361522
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.
Upvotes: 3
Reputation: 49251
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));
Upvotes: 0
Reputation: 133024
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++.
Upvotes: 6
Reputation: 258618
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.
Upvotes: 6