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 1 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.

    P Offline
    P Offline
    PIEBALDconsult
    wrote on last edited by
    #11

    Very large values of 7. Or : "And 1 for good luck"

    modified on Tuesday, September 2, 2008 9:45 PM

    1 Reply Last reply
    0
    • C codemunch

      I know it's not C++, but if it's any consolation, VS 2008 w/ C# and .net 3.5 says c == 21

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #12

      codemunch wrote:

      VS 2008 w/ C# and .net 3.5 says c == 21

      :omg: :wtf: Personally I never use that (++a), except after careful consideration.

      xacc.ide - now with TabsToSpaces support
      IronScheme - 1.0 alpha 4a out now (29 May 2008)

      P 1 Reply Last reply
      0
      • P PIEBALDconsult

        HP C V7.3-009 on OpenVMS Alpha V8.3 reports: int c = ++a + ++a + ++a; ............^ %CC-W-UNDEFVARMOD, In the initializer for c, the expression "++a+++a+++a" modifies the variable "a" more than once without an intervening sequence point. This behavior is undefined. and yields 21.

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

        Yes, I agree. The correct answer is 'undefined'. What a ridiculous question to set in the first place. Shame on you, Mr Examiner, shame on you.

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

        1 Reply Last reply
        0
        • J Justin Cooke

          Wow, that really is a silly (read bad) exam question. It's the poster child example of an expression with an undefined result (it can be different on different compilers). You can see in the ISO C++ standard here http://www.kuzbass.ru:8086/docs/isocpp/expr.html[^] (look at 5.4) that you can't know the value of this expression. I agree with the others that 21 (6 + 7 + 8) and 24 (8 + 8 + 8) make some kind of sense, but I've got no idea where 22 came from either. Show the standard to your prof, if you're in the mood to argue :). Thanks, Justin

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

          It might be that the point of this quesiton WAS to identify the result as undefined.

          Justin Cooke wrote:

          but I've got no idea where 22 came from either

          the increment of a may take place at arbitrary points, so this is entirely possible. However, "result is undefined " means 42 is valid, too :D

          We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
          blog: TDD - the Aha! | Linkify!| FoldWithUs! | sighist

          P 1 Reply Last reply
          0
          • P peterchen

            It might be that the point of this quesiton WAS to identify the result as undefined.

            Justin Cooke wrote:

            but I've got no idea where 22 came from either

            the increment of a may take place at arbitrary points, so this is entirely possible. However, "result is undefined " means 42 is valid, too :D

            We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
            blog: TDD - the Aha! | Linkify!| FoldWithUs! | sighist

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #15

            peterchen wrote:

            It might be that the point of this quesiton WAS to identify the result as undefined

            That's the only reason I would ask it, maybe as extra credit or something.

            1 Reply Last reply
            0
            • P PIEBALDconsult

              HP C V7.3-009 on OpenVMS Alpha V8.3 reports: int c = ++a + ++a + ++a; ............^ %CC-W-UNDEFVARMOD, In the initializer for c, the expression "++a+++a+++a" modifies the variable "a" more than once without an intervening sequence point. This behavior is undefined. and yields 21.

              J Offline
              J Offline
              Jerry Jeremiah
              wrote on last edited by
              #16

              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 1 Reply Last reply
              0
              • L leppie

                codemunch wrote:

                VS 2008 w/ C# and .net 3.5 says c == 21

                :omg: :wtf: Personally I never use that (++a), except after careful consideration.

                xacc.ide - now with TabsToSpaces support
                IronScheme - 1.0 alpha 4a out now (29 May 2008)

                P Offline
                P Offline
                Paul Conrad
                wrote on last edited by
                #17

                I agree. I tend to stick with a++, increment afterwards, not before. That is just asking for weirdness to creep in.

                "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon "Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham

                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

                  M Offline
                  M Offline
                  Marc Clifton
                  wrote on last edited by
                  #18

                  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 L 2 Replies Last reply
                  0
                  • F Fer Simoes

                    this is my idea of how 22 came up, by looking at the disassembly the compiler gets the first '++a' and increment a from 5 to 6 then, as thios is a sum it needs the second operand, so get the next '++a' and increments a from 6 to 7. But, when triyng to make the sum it sees 'a' as the two operands for the sum so it uses the last value of 'a' (that is 7) and store it as a result the sum so far is 14 then it sees another su operation with operands 'last_result' and ++a so it increments 'a' from 7 to 8 and sum(last_result + a) giving a value of 22 silly. isn“t it?

                    Regards Fer Simoes

                    D Offline
                    D Offline
                    dighn
                    wrote on last edited by
                    #19

                    kinda makes sense if you break down the expression into sum(sum(++a, ++a), ++a)

                    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

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #20

                      Fer Simoes wrote:

                      Visual Studio 2005 says c == 24

                      That's what was also my initial guess.

                      Fer Simoes wrote:

                      Visual C++ 6.0 says c == 22

                      I'm not really surprised by that. I mean, it's VC++6, the result could have also been 183 or -55. regards

                      1 Reply Last reply
                      0
                      • 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