-1

doubles seem not to work. can I only use int?
I heard that I can use C++ function templates to change this to double. I'm not sure how to go about that though.

 #include <iostream>                                                     // Necessary 
using namespace std;
#define mMaxOf2(max, min) ((max) > (min) ? (max) : (min))
#define mMaxOf3(Min, Mid, Max)\
{\
     mMaxOf2(mMaxOf2((Min), (Mid)),(Max))\
}
inline long double fMaxOf2(long double min, long double max)
{
    return max > min ? max : min;
}

inline long double fMaxOf3(long double Min, long double Mid, long double Max)
{
      return fMaxOf2(Min, fMaxOf2( Mid, Max));
    //fMaxOf2(Min, fMaxOf2( Mid, Max));     caused nan   problem      
}

int main()
{
    double primary;
    double secondary;
    double tertiary;

    cout << "Please enter three numbers: ";
    cin >> primary >> secondary >> tertiary;
    cout << "The maximum of " << primary << " " << secondary << " " << tertiary;

    long double maximum = mMaxOf3(primary, secondary, tertiary);
    cout << " using mMaxOf3 is " << maximum;

    cout << "\nThe maximum of " << primary << " " << secondary << " " << tertiary;
    long double maxim = fMaxOf3(primary, secondary, tertiary);
    cout << " using fMaxOf3 is " << maxim;

    return 0;
}

So the problem was

    inline long double fMaxOf2(long double min, long double max)
    {
        return max > min ? max : min;
    }

    inline long double fMaxOf3(long double Min, long double Mid, long double Max)
    {
        fMaxOf2(Min, fMaxOf2( Mid, Max));    // This was wrong
        // It was fMaxOf2 (fMaxOf2(Min, Mid, Max);
    }

Anyway now I get a new error... says maxim is nan... Solved it. Thanks Everyone!

3 Answers3

3

Using templates:

template<class T>
inline T fMaxOf2(T min, T max)
{
    return max > min ? max : min;
}

template<class T>
inline T fMaxOf3(T Min, T Mid, T Max)
{
    fMaxOf2(Min, fMaxOf2(Mid, Max));
}

To use the functions:

double max = fMaxOf3<double>(0.231, 123.21312, 904.4);

Now you may ask, why is that? Templates accepts, well, template arguments. T in template<class T> is the argument for the two functions. T now can be used in your functions as a "normal" type or class.

Paul R
  • 208,748
  • 37
  • 389
  • 560
Mark Garcia
  • 17,424
  • 4
  • 58
  • 94
  • Wow. Nice, I wouldn't have thought of that. Thank You. – user1728737 Oct 22 '12 at 06:11
  • @user1728737 Your welcome. I suggest you read all the good C++ books you can to sharpen your skills. See: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list. – Mark Garcia Oct 22 '12 at 06:13
1

It is not an inline problem, you're missing type declarations for your functions. It should be :

inline double fMaxOf2(double min, double max)
{
    return max > min ? max : min;
}

inline double fMaxOf3(double Min, double Mid,double Max)
{
    fMaxOf2(Min, fMaxOf2(Mid, Max));
}
Paul R
  • 208,748
  • 37
  • 389
  • 560
user18428
  • 1,216
  • 11
  • 17
  • Yes. It's correct. Inline functions really have just the same syntax as "normal" functions (except for `inline` of course). – Mark Garcia Oct 22 '12 at 06:16
0

Ur code in fMaxOf3 is wrong since fMaxOf2 takes only 2 arguments.

template <class T>
    inline T fMaxOf2(T min, T max)
    {
        return max > min ? max : min;
    }

    template <class T>
    inline T fMaxOf3(T Min, T Mid,T Max)
    {
        fMaxOf2(Min,fMaxOf2(Mid, Max));
    }
anand
  • 11,071
  • 28
  • 101
  • 159
  • Was this meant to be a comment on an earlier answer ? – Paul R Oct 22 '12 at 06:16
  • It seems to be just a re-post of @Mark's earlier answer with a minor correction - it should have been a comment since it doesn't provide any new information. (I would have given it a second down-vote if I could, for use of txt-speak.) – Paul R Oct 22 '12 at 06:19
  • @Paul: this was posted at same time with othe comment, no need to downvote it..or else leave it as it is – anand Oct 22 '12 at 06:20
  • I disagree - it's a low quality answer posted several minutes after a better answer containing identical code. – Paul R Oct 22 '12 at 06:23
  • why there is upvote on @user1654209 answer..I completely diagree with Paul.Mark answer was not even correct. – anand Oct 22 '12 at 06:50
  • Mark gave me the information I was looking for, where others didn't . But others did help me so I appreciate that.. Thanks for your help. – user1728737 Oct 22 '12 at 07:09
  • This is what I ended up with: return fMaxOf2(fMaxOf2(Min, Mid), Max); It works prperly. – user1728737 Oct 22 '12 at 07:10