On 12/26/2009 5:57 AM, Anup Joshi wrote:
> Hello friends,
> I wanted to find biggest number of given two numbers without using the
> loop statement, the if-conditional, or the ternary operator ?:. One way
> i could do this was with the following code:
>
> int biggest(int a, int b) {
> int result;
>
> (a<b)&& (result = b); // b is bigger
> (a>b)&& (result =a); // a is bigger
> (a==b)&& (result = a) // both are equal
>
> return result;
> }
>
> this code works for positive as well as negative ints.
> i wanted to know if there was any other way to find the biggest number
> of the two, by using any other c operator or snippet.
>
If you look in your standard headers (I think math.h or cmath) you will
find macros called MIN and MAX. They typically use the ternary operator,
and reduce your logic above to two paths. You can combine the second and
third conditions into a single one. You could define an inline function
like this:
inline int max(int a, int b) {
return a > b ? a : b;
}
If this is for C++ I would use a template function so it works with
other data types.
--
John Gaughan
http://www.jtgprogramming.org/