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. Other Discussions
  3. Clever Code
  4. strange sum

strange sum

Scheduled Pinned Locked Moved Clever Code
c++csharpvisual-studioquestion
27 Posts 19 Posters 30 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.
  • E ednrg

    24 makes perfect sense to me. I have no idea where 22 came from.

    L Offline
    L Offline
    Lutoslaw
    wrote on last edited by
    #21

    A harmonic mean of numbers 21 and 24 is 22.4, so the VC compiler takes a floor(harm_mean) as the most expected value.

    Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.

    1 Reply Last reply
    0
    • M Marc Clifton

      Fer Simoes wrote:

      The question was: write the final result for this sum: int a = 5; int c = ++a + ++a + ++a;

      Answer 1: I don't write crap code like that Answer 2: If your programmers do, I'm not working here Marc

      Thyme In The Country Interacx My Blog

      F Offline
      F Offline
      Fer Simoes
      wrote on last edited by
      #22

      sorry marc, it seems you did not understand the meaning of the post. Nobody here writes crap code :b, it is only for fun

      Regards Fer Simoes

      1 Reply Last reply
      0
      • F Fer Simoes

        hi everybody, I have recently run into this when taking a silly C++ exam The question was: write the final result for this sum: int a = 5; int c = ++a + ++a + ++a; logically I Thought the result would be 21 and I tested it in two compilers: Visual C++ 6.0 says c == 22 Visual Studio 2005 says c == 24 I hop you enjoy this as much as I. Greetings

        Regards Fer Simoes

        A Offline
        A Offline
        asadullah ansari
        wrote on last edited by
        #23

        This sort of thing also dependent on how compiler inplements it, like left to right or right to left. If from left to right or left to right c= ++a means c=6 now a=6 c=6+ ++a means c=7 now a=7 c=13+ ++a means c=8 now a=8 c=13+8 c=21 this results on GCC/g++ compiler. But some compiler evaluate first and then assignment like ++a=>>>a=6 ++a=>>a=7 ++a=>>>a=8 Now c=8+8+8=24 But i dont know how it's giving 22.. No points to give 22 as i think...

        Truth Can'nt be changed

        S 1 Reply Last reply
        0
        • F Fer Simoes

          hi everybody, I have recently run into this when taking a silly C++ exam The question was: write the final result for this sum: int a = 5; int c = ++a + ++a + ++a; logically I Thought the result would be 21 and I tested it in two compilers: Visual C++ 6.0 says c == 22 Visual Studio 2005 says c == 24 I hop you enjoy this as much as I. Greetings

          Regards Fer Simoes

          A Offline
          A Offline
          asadullah ansari
          wrote on last edited by
          #24

          This sort of problem we should not bothered. It can give any result depends on compiler inplements it how???

          Truth Can'nt be changed

          1 Reply Last reply
          0
          • M Marc Clifton

            Fer Simoes wrote:

            The question was: write the final result for this sum: int a = 5; int c = ++a + ++a + ++a;

            Answer 1: I don't write crap code like that Answer 2: If your programmers do, I'm not working here Marc

            Thyme In The Country Interacx My Blog

            L Offline
            L Offline
            Le centriste
            wrote on last edited by
            #25

            I think it makes sense as a C++ exam question, to see if the student understands the operator precedence. I myself never rely on operator precedence, I always use parentheses.

            1 Reply Last reply
            0
            • A asadullah ansari

              This sort of thing also dependent on how compiler inplements it, like left to right or right to left. If from left to right or left to right c= ++a means c=6 now a=6 c=6+ ++a means c=7 now a=7 c=13+ ++a means c=8 now a=8 c=13+8 c=21 this results on GCC/g++ compiler. But some compiler evaluate first and then assignment like ++a=>>>a=6 ++a=>>a=7 ++a=>>>a=8 Now c=8+8+8=24 But i dont know how it's giving 22.. No points to give 22 as i think...

              Truth Can'nt be changed

              S Offline
              S Offline
              sucram
              wrote on last edited by
              #26

              It gets 22 like this: ++a++ + a + ++a 7+7+8=22

              1 Reply Last reply
              0
              • J Jerry Jeremiah

                You can tell when a company goes to a lot of effort to make their compiler easy to use. The most expensive part of a project is the amount of debugging and fixing bugs. A compiler that can save this much time is a Good Thing.

                P Offline
                P Offline
                Paul Sanders the other one
                wrote on last edited by
                #27

                Yes, good point. The example given is extreme and nobody in their right mind would code that way but I imagine more subtle examples that might slip through on a Friday afternoon are not hard to think up: int i = ++i1 + ++i1; // whoops, meant ++i2

                Paul Sanders http://www.alpinesoft.co.uk

                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