0

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;
             }
        }
}
6
Contributors
7
Replies
8
Views
5 Years
Discussion Span
Last Post by Nick Evan
0

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;
}

Edited by NathanOliver: n/a

0

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.

0

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

Edited by Nick Evan: Added CODE tags

0

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.

Edited by TehCPP: n/a

0

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 );
}
0

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 );
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.