Results 1 to 10 of 10
- 09-13-2011, 03:15 AM #1
Member
- Join Date
- Sep 2011
- Posts
- 3
- Rep Power
- 0
Looking for explanation of equation
I cannot figure out the equation below:
int a = 20;
int b = 10;
a = (a + b) / 2;
b = a++;
The correct answer is (a=16; b=15)
However, I come out to (a=15; b=16). I've been trying to figure out what I'm missing here; can anyone offer a pseudocode breakdown of how this equation results in a=16, b=15?
- 09-13-2011, 03:23 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: Looking for explanation of equation
The postfix operator works different than you may expect. It returns the value it currently is, and then increments the value.
This is how it would look. Notice that it returns a's original value, and then increments a. ++a(prefix) will first increment a, then return it.Java Code:public class Increment{ int a; public int increment(int a){ int x = a; a = a+1; return x; } }
- 09-13-2011, 03:28 AM #3
Member
- Join Date
- Sep 2011
- Posts
- 12
- Rep Power
- 0
Re: Looking for explanation of equation
yeah the way I was taught it was that incrementations are done after setting the variable,
ie
it does
b=a
a++
- 09-13-2011, 03:42 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: Looking for explanation of equation
As popeus pointed out; you can avoid a great amount of confusion by simply putting increments on a seperate line. If you don't truly understand them, avoid getting fancy with them. It's much easier to read something like
As opposed toJava Code:a++; int x = a; x--; int c = x c++ System.out.println(x + ", " + a + ", " + c);
Java Code:int x = ++a; int c = --x; System.out.println(x + ", " + a + ", " + ++c);
- 09-13-2011, 04:06 AM #5
Member
- Join Date
- Sep 2011
- Posts
- 3
- Rep Power
- 0
Re: Looking for explanation of equation
Thank you for your responses; I'm embarrassed to say however, that I didn't understand them. This is my second week in my Java class...I've been cruising until I hit this equation. This is how I am logically breaking it down...tell me where I'm wrong.
int a = 20;
int b = 10;
a = (a + b) / 2;
b = a++;
new variable "a" is given a value of 20;
new variable "b" is given a value of 10;
variable a is assigned a new value of (20+10) divided then by 2 = 15.
So now, "a" has the value of 15.
variable "b" is then assigned a new value, variable "a" increased by 1....so 15 + 1 = 16
End of my logic.
So I can't figure how "b" = 15 and how "a" gets assigned the value of 16.
Taking a look at my logic, can you tell me where I'm going wrong?
- 09-13-2011, 04:57 AM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: Looking for explanation of equation
The increment operator '++' has a side effect. This side effect chagezthevariable it's applied to. If you put 'a++' on it's own line a would be increased by 1(it's value is changed) depending on whether it is postfix 'a++' or prefix '++a' the order of operations is different. Postfix will show the value of a before it's changed, and then change a. The prefix operator changes it and then returns the changed variable.
Your error comes at the following point: 'b=a++' this does two things; first it assigns a to b, so since a is 15, b becomes 15, then it increments a, changing a to 16.
Try this
I'm hoping this may help you understand the difference between postfix and prefix upon running it.Java Code:int a=b=5; System.out.println("postfix: " + a++); System.out.println("prefix: " + ++a);
- 09-13-2011, 05:03 AM #7
Member
- Join Date
- Sep 2011
- Posts
- 3
- Rep Power
- 0
Re: Looking for explanation of equation
Thank you very much...I didn't realize the a++ could reassign a value to the variable "a". I'm going to readu of the postfix and prefix operators... that should get me back on track.
Thanks again... good stuff.
- 09-13-2011, 05:03 AM #8
Member
- Join Date
- Sep 2011
- Posts
- 23
- Rep Power
- 0
Re: Looking for explanation of equation
I am no expert but i ran your code and i got a=16; b=15 just fine?
output:Java Code:int a = 20; int b = 10; a = (a + b) / 2; b = a++; System.out.println(a); System.out.println(b);
16
15
BUILD SUCCESSFUL (total time: 1 second)
- 09-13-2011, 05:06 AM #9
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: Looking for explanation of equation
His problem was that when evaluating it in his head he got a=15,b=16
@op: postfix increment is the same as
Java Code:a=a+1;
- 09-13-2011, 05:08 AM #10
Member
- Join Date
- Sep 2011
- Posts
- 23
- Rep Power
- 0
Similar Threads
-
Help with equation
By _Jk_ in forum New To JavaReplies: 10Last Post: 03-17-2011, 08:15 PM -
equation
By bobo67 in forum New To JavaReplies: 5Last Post: 09-06-2010, 06:40 PM -
Need help with math equation
By annabellastorm in forum New To JavaReplies: 4Last Post: 01-10-2010, 05:12 PM -
differential equation RK4
By arvindmer in forum New To JavaReplies: 3Last Post: 01-08-2009, 01:27 PM -
Quadratic Equation
By jpnym15 in forum New To JavaReplies: 4Last Post: 11-12-2008, 03:29 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks