Hello, my programming project requires me to write an overloaded function max that takes two or three parameters and returns the largest of them. I'm having trouble with the program outputting the largest number, with my code below it just says the largest number is 0 no matter what numbers I input.

Can anyone help? Thanks!

#include <iostream>
using namespace std;

double max(double a, double b, double c);

int main()
{
    double a, b, c;
    
          cout << "Please input 2 or 3 numbers and the program will output\n";
          cout << "the largest number." << endl << endl;
          cin >> a >> b >> c;
          cout << "The largest number is: " << max(a, b, c) << endl << endl;
    
    
    
    
          system("pause");
          return (0);   
}

double max(double a, double b, double c)
{
       
       if(a > b > c)
        {
            if(a > c > b)
              {
                  return a;
              }  
        }
       else if (b > a > c)
        { 
            if(b > c > a) 
             {
                  return b;
             }     
        }
       else if (c > a > b)
        {
            if(c > b > a) 
             {
                  return c;
             }
        }
}

Recommended Answers

All 7 Replies

what happens if you try

double max(double a, double b, double c)
{
    if (a > b && a > c)
        return a;
    if (b > a && b > c)
        return b;
    return c;
}

You can't compare in C++ like you do in math. This if(a > b > c) is wrong, it gets evaluated to (a>b) > c . So if a>b, it returns true, and it compare if true is > c. What you want is if(a > b && b > c) But I suggest something more easier, namely make a max(double a, double b) version. Then use that in max(double a, double b, double c) .

You can use the 2 parameter version in the 3 parameter version by first finding the max of a and b, then using that result to find the max of the result and c, and return that new result.

Here's a little pointer

if (a > b)
{
    if (a > c)
    {
        //highest value is a
    }
    else if (a < c)
    {
       //highest is c
    }

do the same for b and c and you should be fine. I'm not up for the idea of a > b > c cause normally if we break comparisons into parts, we'll only compare 2 together then comparing the result to the next value. I reccomend firstPerson's suggestion of just having 2 parameters cause the algorithm is easier that way. more parameters = more nested if's, not rly pretty to write

Hmm, for me, i'll do it this way.

double max(double a, double b, double c)
    {
    double max=a;
    
    if(b>max)
        max = b;
    if(c>max)
        max = c;
    return max;
    }

It might not be the best solution but i find it easier this way.

ahh true, that saves a lot of code, mine's tedious lol

Hmm, for me, i'll do it this way.

double max(double a, double b, double c)
    {
    double max=a;
    
    if(b>max)
        max = b;
    if(c>max)
        max = c;
    return max;
    }

It might not be the best solution but i find it easier this way.

Thats the general idea, although it would be more clearer if you overload max for two inputs.

double max(double a, double b){ 
 if(a > b) return a;
 else return b;
}
double max(double a, double b, double c){
 return max( max(a,b) , c );
}

Yeah, we need more obfuscation:

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

double max(double a, double b, double c){
 return max( max(a,b) , c );
}
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.