Page 1 of 2 12 LastLast
Results 1 to 20 of 25
  1. #1
    mhamill2 is offline Member
    Join Date
    Jun 2015
    Posts
    16
    Rep Power
    0

    Default Help completing my method, IntegerAverage.

    Hey guys, I am still fairly new to programming and need help with a method.

    Alright, so I need to develop a method that can take the average of any integer, without converting to a larger type and without going over the integer max (so a simple (i+j)/2 solution will not work here). Here is what I have so far:

    Java Code:
         * @param j
         *            the first of two integers to average
         * @param k
         *            the second of two integers to average
         * @return the integer average of j and k
         */
        public static int average(int j, int k) {
            int i = 0;
            // if the numbers are equal, then the average is either of the two numbers.
            if (k == j) {
                i = k;
             // if j is the larger number, subtract k from j, divide the result by two, and add k.
            } else if (j > k) {
                i = j - k;
                i = i / 2;
                i += k;
             // if k is the larger number, subtract j from k, divide the result by two, and add j.
            } else if (k > j) {
                i = k - j;
                i = i / 2;
                i += j;
            }
            return i;
        }
            }
    This almost works, except for a few special cases, some of which are:

    i = -5, j = -8
    i = -3, j = 2
    i = 1, j = -50
    i = -2, j = -1

    These test cases all give me 1 less than what the average should be, but I cannot figure out what the special case is here.
    Last edited by mhamill2; 01-15-2016 at 06:10 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: Help completing my method, IntegerAverage.

    Can you add some comments to the code describing what it is doing and how it is trying to do it? What values are contained in the variables: i,j,k?
    In other words describe the algorithm you are following to solve the problem.

    Please edit your post and wrap your code with code tags:
    [code]
    **YOUR CODE GOES HERE**
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    mhamill2 is offline Member
    Join Date
    Jun 2015
    Posts
    16
    Rep Power
    0

    Default Re: Help completing my method, IntegerAverage.

    Alright, I went ahead and made my code a little more clear. Thanks.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: Help completing my method, IntegerAverage.

    works, except for a few special cases,
    Have you tried to general those special cases to ranges of values or something?
    Do just those sets of numbers fail?
    Could same sign test and absolute values be used?

    Have you worked though the logic manually using each case where it fails?
    That would help you to see what the code doing.
    Last edited by Norm; 01-15-2016 at 06:31 PM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    mhamill2 is offline Member
    Join Date
    Jun 2015
    Posts
    16
    Rep Power
    0

    Default Re: Help completing my method, IntegerAverage.

    I have tried using absolute values, but problems still occur with other test cases. The problem doesn't occur because they are both negative numbers, or because one of the numbers is negative and the other is positive.

  6. #6
    mhamill2 is offline Member
    Join Date
    Jun 2015
    Posts
    16
    Rep Power
    0

    Default Re: Help completing my method, IntegerAverage.

    I have worked through each case and why they fail. They fail because of the way integer rounds numbers, but I can't figure out what the similarity is between the cases in which this problem occurs. It's not because the difference is even or odd, not because the difference divided by two is even or odd. I don't know what it could be.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: Help completing my method, IntegerAverage.

    They fail because of the way integer rounds numbers,
    Yes the average of an odd total will require a .5 and that is not an int value.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    mhamill2 is offline Member
    Join Date
    Jun 2015
    Posts
    16
    Rep Power
    0

    Default Re: Help completing my method, IntegerAverage.

    I understand that, but what does that tell me? The code still works when the difference is an odd number, but sometimes it doesn't. I can't figure out what the special case is as to why that happens.

    For example, it works with 8 and 5, but not -8 and -5. The reason it doesn't work isn't because i'm using two negative numbers though.
    Last edited by mhamill2; 01-15-2016 at 08:16 PM.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: Help completing my method, IntegerAverage.

    it works with 8 and 5, but not -8 and -5. The reason it doesn't work isn't because i'm using two negative numbers
    What do you see when you manually work through the code with the numbers that do not work compared with the numbers that do work?
    Can you post the code with the values to show what happens with each case?
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    mhamill2 is offline Member
    Join Date
    Jun 2015
    Posts
    16
    Rep Power
    0

    Default Re: Help completing my method, IntegerAverage.

    So, it looks like the problem arises when the average is negative and the true mathematical average is not an integer. In that case, the code rounds the wrong way. Now, how could I check for that without using (i+j)/2?

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: Help completing my method, IntegerAverage.

    the code rounds the wrong way
    Can you give an example? Show the steps where the code rounds the wrong way.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    mhamill2 is offline Member
    Join Date
    Jun 2015
    Posts
    16
    Rep Power
    0

    Default Re: Help completing my method, IntegerAverage.

    All of the test cases I gave in the first post.

    Example:
    -5 - (-8) = 3
    3/2 = 1
    1 + (-8) = -7

    Now, with integer division, the average should give -6, but my algorithm gives me -7 in this case. As you can see the true mathematical average of -5 and -8 is a negative number, and it is not an integer (-6.5). So my algorithm, in this case, rounds the wrong way.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: Help completing my method, IntegerAverage.

    So my algorithm, in this case, rounds the wrong way.
    Ok, time to find an algorithm that works.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    mhamill2 is offline Member
    Join Date
    Jun 2015
    Posts
    16
    Rep Power
    0

    Default Re: Help completing my method, IntegerAverage.

    Right. Which is what i can't seem to figure out.

    I've tried using absolute value, testing for negative numbers, testing for one negative number, checking for a odd difference and a negative number, but when one problem is fixed another arises. I don't know what to do.

  15. #15
    mhamill2 is offline Member
    Join Date
    Jun 2015
    Posts
    16
    Rep Power
    0

    Default Re: Help completing my method, IntegerAverage.

    Honestly, I have no idea where to go from here.

  16. #16
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Help completing my method, IntegerAverage.

    Quote Originally Posted by mhamill2 View Post
    I don't know what to do.
    You need to think about what you know for certain. For two numbers, say 15 and 20 you want to find
    (15 + 20)/2 as close as possible. You know that must be a difference of 1.

    quotient1 = 15/2 = 7 (ignore remainder)
    quotient2 = 20/2 = 10 (ignore remainder)

    Notice that quotient1 + quotient2 = 17
    And 17 + 17 is off by 1 from 20 + 15.

    Now you just need to figure out under what situations do you add a correction factor to quotient sums to get the
    close average. You can either do this with pencil and paper or generate some test numbers and print intermediate
    results to see if something stands out.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: Help completing my method, IntegerAverage.

    FWIW a rewrite of the code:
    Java Code:
        public static int average(int num1, int num2) {  
            int save = num1;          // if the numbers are equal, then the average is either of the two numbers.
            // if different take difference, div by 2 and add lower
            if (num1 != num2) {
               int bigger = num1;  // assume   or  bigger = num1 > num2 ? num1 : num2
               int smaller = num2;
               if(num2 > num1) {   // change if wrong
                  bigger = num2;
                  smaller = num1;
               } 
               save = bigger - smaller;
               save /= 2;
               save += smaller;
            }
            return save;
       }
    where to go from here.
    What simple change would give the correct answer for your test cases? Does adding +1 gives the right answer?
    If you don't understand my response, don't ignore it, ask a question.

  18. #18
    mhamill2 is offline Member
    Join Date
    Jun 2015
    Posts
    16
    Rep Power
    0

    Default Re: Help completing my method, IntegerAverage.

    While it would give the correct answer for the test cases I posted, it would not work for others. Such as num1 = 11 and num2 = -4. I feel like the answer is right in front of me, but I can't seem to grasp it.

  19. #19
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Help completing my method, IntegerAverage.

    Num1 = 11
    Num2 = -4

    q1 = 11/2 = 5 (ignore remainder of 1)
    q2 = -4/2 = -2 (ignore remainder of 0 )

    avg1 = 5 + (-2) = 3
    avg2 = 5 + (-2) = 3

    avg1 = avg1 + 1; // add one remainder from earlier
    avg2 = avg2 + 0; // add the other remainder from earlier

    avg1 = 4
    avg2 = 3

    11 + -4 = 7
    avg1 + avg2 = 7

    Done!

    Now, under what situations did you (or will you) add the remainders? The remainders could be as follows:
    0, 0
    0, 1
    0, -1
    -1, -1
    1, -1

    Remember that it doesn't matter to which average you add the remainders because they are the same until you
    "correct" their values.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  20. #20
    mhamill2 is offline Member
    Join Date
    Jun 2015
    Posts
    16
    Rep Power
    0

    Default Re: Help completing my method, IntegerAverage.

    Ahh I think I got it. How does this look?

    Java Code:
    /**
         * Returns the integer average of two given {@code int}s.
         *
         * @param j
         *            the first of two integers to average
         * @param k
         *            the second of two integers to average
         * @return the integer average of j and k
         * @ensures average = (j+k)/2
         */
        public static int average(int j, int k) {
            int avg = j;
            if (j != k) {
                int bigger = j;
                int smaller = k;
                if (k > j) {
                    bigger = k;
                    smaller = j;
                }
                avg = bigger - smaller;
                avg /= 2;
                avg += smaller;
                if (smaller < 0 && bigger + smaller < 0
                        && (bigger + smaller) % 2 != 0) {
                    avg++;
                }
            }
            return avg;
        }

Page 1 of 2 12 LastLast

Similar Threads

  1. Completing the method body
    By Michael305rodri in forum New To Java
    Replies: 16
    Last Post: 11-04-2012, 06:58 AM
  2. Replies: 6
    Last Post: 12-05-2011, 07:35 PM
  3. Need help in completing this code
    By nn12 in forum New To Java
    Replies: 9
    Last Post: 09-09-2008, 07:02 AM
  4. Completing A Program..
    By Louise1875 in forum New To Java
    Replies: 1
    Last Post: 05-11-2008, 06:24 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
  •