Results 1 to 10 of 10
  1. #1
    flyersjoe is offline Member
    Join Date
    Sep 2011
    Posts
    3
    Rep Power
    0

    Default 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?

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

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

    Java Code:
    public class Increment{
      int a;
      public int increment(int a){
        int x = a;
        a = a+1;
        return x;
      }
    }
    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.

  3. #3
    popeus is offline Member
    Join Date
    Sep 2011
    Posts
    12
    Rep Power
    0

    Default 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++

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default 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

    Java Code:
    a++;
    int x = a;
    x--;
    int c = x
    c++
    System.out.println(x + ", " + a + ", " + c);
    As opposed to
    Java Code:
    int x = ++a;
    int c = --x;
    System.out.println(x + ", " + a + ", " + ++c);

  5. #5
    flyersjoe is offline Member
    Join Date
    Sep 2011
    Posts
    3
    Rep Power
    0

    Default 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?

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default 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
    Java Code:
    int a=b=5;
    System.out.println("postfix: " + a++);
    System.out.println("prefix:  " + ++a);
    I'm hoping this may help you understand the difference between postfix and prefix upon running it.

  7. #7
    flyersjoe is offline Member
    Join Date
    Sep 2011
    Posts
    3
    Rep Power
    0

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

  8. #8
    Jason is offline Member
    Join Date
    Sep 2011
    Posts
    23
    Rep Power
    0

    Default Re: Looking for explanation of equation

    I am no expert but i ran your code and i got a=16; b=15 just fine?

    Java Code:
            int a = 20;
            
            int b = 10;
           
            a = (a + b) / 2;
           
             b = a++;
             System.out.println(a);
             System.out.println(b);
    output:
    16
    15
    BUILD SUCCESSFUL (total time: 1 second)

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default 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;

  10. #10
    Jason is offline Member
    Join Date
    Sep 2011
    Posts
    23
    Rep Power
    0

Similar Threads

  1. Help with equation
    By _Jk_ in forum New To Java
    Replies: 10
    Last Post: 03-17-2011, 09:15 PM
  2. equation
    By bobo67 in forum New To Java
    Replies: 5
    Last Post: 09-06-2010, 07:40 PM
  3. Need help with math equation
    By annabellastorm in forum New To Java
    Replies: 4
    Last Post: 01-10-2010, 06:12 PM
  4. differential equation RK4
    By arvindmer in forum New To Java
    Replies: 3
    Last Post: 01-08-2009, 02:27 PM
  5. Quadratic Equation
    By jpnym15 in forum New To Java
    Replies: 4
    Last Post: 11-12-2008, 04:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •