strange sum
-
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
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
-
ednrg wrote:
I have no idea where 22 came from.
The clue is probably somewhere in the phrase Visual C++ ;P Regards, --Perspx
"The Blue Screen of Death, also known as The Blue Screen of Doom, the "Blue Screen of Fun", "Phatul Exception: The WRECKening" and "Windows Vista", is a multi award-winning game first developed in 1995 by Microsoft" - Uncyclopedia Introduction to Object-Oriented JavaScript
-
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 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
-
ednrg wrote:
I have no idea where 22 came from.
The clue is probably somewhere in the phrase Visual C++ ;P Regards, --Perspx
"The Blue Screen of Death, also known as The Blue Screen of Doom, the "Blue Screen of Fun", "Phatul Exception: The WRECKening" and "Windows Vista", is a multi award-winning game first developed in 1995 by Microsoft" - Uncyclopedia Introduction to Object-Oriented JavaScript
Yes. He must have missed that.
"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
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.
-
Very large values of 7. Or : "And 1 for good luck"
modified on Tuesday, September 2, 2008 9:45 PM
-
I know it's not C++, but if it's any consolation, VS 2008 w/ C# and .net 3.5 says c == 21
-
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.
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
-
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
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! | sighist -
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.