Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Maximum function

Maximum function

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
7 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sunnygirl
    wrote on last edited by
    #1

    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

    M T Z 3 Replies Last reply
    0
    • S Sunnygirl

      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

      M Offline
      M Offline
      Melekor
      wrote on last edited by
      #2

      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)

      P 1 Reply Last reply
      0
      • S Sunnygirl

        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

        T Offline
        T Offline
        Toni78
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • M Melekor

          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)

          P Offline
          P Offline
          peterchen
          wrote on last edited by
          #4

          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

          M 1 Reply Last reply
          0
          • S Sunnygirl

            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

            Z Offline
            Z Offline
            ZoogieZork
            wrote on last edited by
            #5

            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 reference max_element code sample from MSDN - Mike

            1 Reply Last reply
            0
            • P peterchen

              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

              M Offline
              M Offline
              majian405
              wrote on last edited by
              #6

              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!

              P 1 Reply Last reply
              0
              • M majian405

                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!

                P Offline
                P Offline
                peterchen
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups