Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm doing a simple exercise (again) to determine the max value of 4 integers. I have a working solution already, I just need help to understand why my first trial with if-else condition wouldn't work correctly.

What I have tried:

C++
int max (int a, int b, int c, int d){
    int result = a;
    if (result > b) return result; else result = b;
    if (result > c) return result; else result = c;
    if (result > d) return result; else result = d;
    return result; 
}
 
INPUT : 6 4 2 13
OUTPUT: 6
This is the working solution, the logic looks quite the same to me, or I might be blind (again).
C++
int max (int a, int b, int c, int d){   
    int result = a;
    result > b ? result : result = b;
    result > c ? result : result = c;
    result > d ? result : result = d;
    return result; 
}
 
INPUT : 6 4 2 13
OUTPUT: 13
Doesn't the operator ?: work similar to conditional anyway, how did the result end up different ? What explanation did I miss here ?
Posted
Updated 13-Oct-21 4:50am

The first solution will return a, if a > b. It does not examine c or d, which could be larger than a.
The second solution calculates max(a, b), then max(max,c) and so forth. This is the correct way to do it.

Please note that the correct way to use the ternary operator is as a "function" that returns a value:

<pre lang="C++">
result = result > b ? result : b;
 
Share this answer
 
v2
Comments
lock&_lock 13-Oct-21 1:26am    
@Daniel Hi, thanks. I might be blind again. But for the first solution, the max (either a or b) will be stored in variable "result" anyway (return result). And then the current result will be passed down to be compared against c, d, etc, no ? UPDATE : I understand now, it's because the return command itself. Thank you.
You may also write:
C++
int max( int a, int b, int c, int d)
{
  return a > b ? ( a > c ? ( a > d ? a : d) : ( c > d ? c : d) ) : ( b > c ? ( b > d ? b : d ) : (c > d ? c : d));
}
 
Share this answer
 
Comments
Richard MacCutchan 13-Oct-21 4:11am    
Nice.
At the time of this writing, here is what your code looks like :
C++
int max (int a, int b, int c, int d){   
    int result = a;
    result > b ? result : result = b;
    result > c ? result : result = c;
    result > d ? result : result = d;
    return result; 
}
Those ternary expressions still don't look right. They should be of the form:
C++
result = ( result > b ) ? result : b;
Also, naming a c++ function max is not a good idea. There is a macro named max in the standard C headers and the Standard Template Library has a template function named max. It would better to use a different name for this function. I prefer to use a function that assigns the result to a value that is passed in. Here's an example :
C++
__inline void AssignMax( int & maxvalue, int newvalue )
{
    if( maxvalue < newvalue )
        maxvalue = newvalue;
}
and then you can rewrite your function like this :
C++
int GetMaxValue( int a, int b, int c, int d )
{
    result = a;
    AssignMax( result, b );
    AssignMax( result, c );
    AssignMax( result, d );
    return result;
};
One advantage to this approach is it does not constantly assign the value to result. It only does so when the argument passed is larger than it.
 
Share this answer
 
Your problem:
C++
<pre>int max (int a, int b, int c, int d){
    int result = a;
    if (result > b) return result; else result = b; // if a>b you return the answer directly without checking c and d 
    if (result > c) return result; else result = c;
    if (result > d) return result; else result = d;
    return result; 
}
 
INPUT : 6 4 2 13
OUTPUT: 6

Quote:
I just need help to understand why my first trial with if-else condition wouldn't work correctly.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Greetings Kind Regards For you to solve the problem may I suggest learning the meaning and behavior of return - Cheerio
 
Share this answer
 
Comments
lock&_lock 13-Oct-21 10:56am    
Hi, I posted this question almost 10 hours ago and there's already an accepted solution. However, thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900