This Math sentance

Pages: 12
I am trying to figure out how to use increment in C++.
Some guy told me to figure out this sentance, I am not sure if I have the right answer, because I can no longer contact him, anyways, I was wondering if anybody knew the answer to this sentance so I can tell if I understand incrementing.

int a, b; a = b = 0; b = ++(a = (++b + 5)++); a = ? b = ?

Here is what I thought: b = 1 a = 7
Am i right or wrong?
Can somebody answer the math problem for me and possibly explain how to figure out that sentance?
test.cpp:8: error: non-lvalue in increment
It's not meant for debugging, I am asking what is the answer to it :)
And yes, I got the very same error, but like I said in the sentance above.
Last edited on
The answer is that the sentence is invalid.
http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.15

Also, it is nonsense. No valid program will ever have stuff that convoluted in it.

Finally, it is a terrible example for teaching purposes. What exactly is the teacher trying to teach? Bad programming? Confusing semantics?

The ++ operators simply increment (or "bump") the integer variable by one. In C++ this also extrapolates to sequence iterators and the like to mean "reference the next item", etc.


The pre-increment operator ++a increments a before its value is used in an expression.

The post-increment operator a++ increments a after its value is used in an expression.

So:
1
2
3
4
5
6
7
8
9
int a = 0;
cout << "a   is " << a   << endl;
cout << "a++ is " << a++ << endl;
cout << "a   is " << a   << endl;

a = 0;
cout << "a   is " <<   a << endl;
cout << "++a is " << ++a << endl;
cout << "a   is " <<   a << endl;
Run that code and things will become clearer.

Hope this helps.
Thank you very much.
You see, there is this person who has a group on this game with that sentance, and he says you need to know the answer to get in. I am not doing it for that, but I didn't know anything about incrementing before I saw that group, so I wanted to learn about it.
That shouldn't compile. (++b + 5)++ is nonsense.
Like I said before, It is not meant to compile, but he wants me to give him an answer.
That is the part i get stuck at, too!
This is exactly what he told me:

"Send me the answer of this in a PM to get in. int a, b; a = b = 0; b = ++(a = (++b + 5)++); a = ? b = ? If you dont get a reply then it was wrong."
Well, the answer is simple: the statement is meaningless, so the values of a and b cannot be determined.
Well, that's weird, because a total of 19 people have been added to the group
Send him "There is no answer. *Show error*"
Some guy who made it in the group said this is the answer:
a=7 and b=8

Anybody know how he got that?
Maybe he applied the ++ as an operation that doesn't have to work on variables, just on numbers...
If that was the case, then the answer should be a=6, b=8.
Well thats not what he told me, and he was allowed in the group.
First of all, any answer will be wrong, because the expression doesn't make sense.
Second, just take a look at it:
1
2
3
4
5
6
7
b=a=0;
b=++(a=(++b + 5)++);
/*
This is (supposedly) the same as:
a=1+5;
b=1+(a+1); //(post-increment)
*/
Last edited on
@helios - wouldn't it be a=(1+5)+1 ... in this persons dream world...
Post. Post increment. b=a++; is the same as b=a; a+=1;, not a+=1; b=a;
Last edited on
Vect, I don't think you really want to be in a group of losers that have no grasp on reality.

(n+5)++ is bunk, since the ++ only works on lvalues -- meaning something you can meaningly modify (like a variable).

n++ is meaningful.
5++ is not.
(2 + 5)++ is not.
(n + 5)++ is not.
(n += 5)++ may or may not, depending on how ancient and derelict your compiler is.

Remember, ++ implies that the operand is modifiable and retrievable. Calculated values fail on the second point. (They are calculated in the processor's registers, copied elsewhere, and then forgotten -- register values are not preserved after the calculation is completed and are likely to be obliterated with the very next operation.)

Here is a real-world example where this very same failure caught someone by surprise:
http://groups.google.com.bz/group/comp.lang.c/msg/bde1c31e563e26c3

As for expressions with stuff like ++n + n++, the language does not guarantee that anything reasonable happens. If n were zero at the beginning of that expression, you might get -59213 as a valid result. (Presuming your compiler didn't complain when attempting to compile it.) Again:
http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.15

Hope this helps.
http://groups.google.com.bz/group/comp.lang.c/msg/bde1c31e563e26c3
Huh. That's a pretty subtle error. I would have cast the two pointers immediately to intermediate pointers, but I guess we was trying to minimize stack frame size for some reason. Perhaps he wanted to increase the chance that the compiler would inline?
Why he didn't just use memcpy() is something left to your imagination.
well isn't there supose to be a value on the increment

so...
a = a++; //right
a = ++; // wrong??
Pages: 12