strange sum
-
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 :DWe 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! | sighistpeterchen 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.
-
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.
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.
-
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)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
-
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
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
-
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
-
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 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.
-
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
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
-
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
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
-
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
This sort of problem we should not bothered. It can give any result depends on compiler inplements it how???
Truth Can'nt be changed
-
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
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.
-
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
-
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.
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