Maximum function
-
hello @all, i need a something, that find the maximum of different int or double values. for example: max{1, 2.5, 2}=2.5 can anybody help me? thank you very much. sunny
use a macro like #define max(a, b) (a > b ? a : b) or if you need a variable ammount of arguments, create a function that uses va_list, like this one (MSDN example)
-
hello @all, i need a something, that find the maximum of different int or double values. for example: max{1, 2.5, 2}=2.5 can anybody help me? thank you very much. sunny
I really haven't tested this code to check for errors but this is the general idea on how to get the maximum number:
int nNoNumbers = SOME_VALUE;
int *piArrayNums = new int[ nNoNumbers ];
...
// Initialize piArrayNums here
...
// Assume that the largest number is the first
// in the array.
int iMax = piArrayNums[ 0 ];
for( i = 0; i < nNoNumbers; i++ )
{
// If the assumption was wrong correct it
if( piArrayNums[ i ] > iMax )
{
iMax = piArrayNums[ i ];
} // end if
} // end for i
// Don't forget to free the memory allocated for the array.
// In case you use new.The same thing can be done with doubles. // Afterall, I realized that even my comment lines have bugs When one cannot invent, one must at least improve (in bed).-My latest fortune cookie
-
use a macro like #define max(a, b) (a > b ? a : b) or if you need a variable ammount of arguments, create a function that uses va_list, like this one (MSDN example)
just two notes a) you shouldn't use a macro if it can be expressed in the language - i.e.
template <typename T> inline T max(T a, T b) { return (a > b) ? a : b; }
should do it without side effect problems, correct name scoping b) if you need to use a macro, use braces around each argument,#define max(a,b) ((a) > (b) ? (a) : (b))
"Der Geist des Kriegers ist erwacht / Ich hab die Macht" StS
sighist | Agile Programming | doxygen -
hello @all, i need a something, that find the maximum of different int or double values. for example: max{1, 2.5, 2}=2.5 can anybody help me? thank you very much. sunny
The STL provides a
std::max_element
template function that operates on a collection of any type of comparable value.max_element
from SGI's STL referencemax_element
code sample from MSDN - Mike -
just two notes a) you shouldn't use a macro if it can be expressed in the language - i.e.
template <typename T> inline T max(T a, T b) { return (a > b) ? a : b; }
should do it without side effect problems, correct name scoping b) if you need to use a macro, use braces around each argument,#define max(a,b) ((a) > (b) ? (a) : (b))
"Der Geist des Kriegers ist erwacht / Ich hab die Macht" StS
sighist | Agile Programming | doxygenUse macro is not a good choice.You can find this in <> Item 1 : #define max(a,b) ((a) > (b) ? (a) : (b)) This little number has so many drawbacks, just thinking about them is painful. You're better off playing in the freeway during rush hour. Whenever you write a macro like this, you have to remember to parenthesize all the arguments when you write the macro body; otherwise you can run into trouble when somebody calls the macro with an expression. But even if you get that right, look at the weird things that can happen: int a = 5, b = 0; max(++a, b); // a is incremented twice max(++a, b+10); // a is incremented once Here, what happens to a inside max depends on what it is being compared with!
-
Use macro is not a good choice.You can find this in <> Item 1 : #define max(a,b) ((a) > (b) ? (a) : (b)) This little number has so many drawbacks, just thinking about them is painful. You're better off playing in the freeway during rush hour. Whenever you write a macro like this, you have to remember to parenthesize all the arguments when you write the macro body; otherwise you can run into trouble when somebody calls the macro with an expression. But even if you get that right, look at the weird things that can happen: int a = 5, b = 0; max(++a, b); // a is incremented twice max(++a, b+10); // a is incremented once Here, what happens to a inside max depends on what it is being compared with!
I know of that - That's what's commonly called "side effects" (I was referring to in my post) - that's why (mostly9 the first instruction never to use macros unless you cannot do without ;)
"Der Geist des Kriegers ist erwacht / Ich hab die Macht" StS
sighist | Agile Programming | doxygen