Results 1 to 20 of 25
 01152016, 08:05 AM #1Member
 Join Date
 Jun 2015
 Posts
 16
 Rep Power
 0
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; } }
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; 01152016 at 06:10 PM.
 01152016, 01:17 PM #2
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.
 01152016, 06:11 PM #3Member
 Join Date
 Jun 2015
 Posts
 16
 Rep Power
 0
Re: Help completing my method, IntegerAverage.
Alright, I went ahead and made my code a little more clear. Thanks.
 01152016, 06:22 PM #4
Re: Help completing my method, IntegerAverage.
works, except for a few special cases,
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; 01152016 at 06:31 PM.
If you don't understand my response, don't ignore it, ask a question.
 01152016, 06:29 PM #5Member
 Join Date
 Jun 2015
 Posts
 16
 Rep Power
 0
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.
 01152016, 06:35 PM #6Member
 Join Date
 Jun 2015
 Posts
 16
 Rep Power
 0
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.
 01152016, 06:49 PM #7
Re: Help completing my method, IntegerAverage.
They fail because of the way integer rounds numbers,If you don't understand my response, don't ignore it, ask a question.
 01152016, 08:13 PM #8Member
 Join Date
 Jun 2015
 Posts
 16
 Rep Power
 0
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; 01152016 at 08:16 PM.
 01152016, 08:47 PM #9
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
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.
 01152016, 10:56 PM #10Member
 Join Date
 Jun 2015
 Posts
 16
 Rep Power
 0
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?
 01152016, 11:21 PM #11
Re: Help completing my method, IntegerAverage.
the code rounds the wrong wayIf you don't understand my response, don't ignore it, ask a question.
 01152016, 11:29 PM #12Member
 Join Date
 Jun 2015
 Posts
 16
 Rep Power
 0
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.
 01152016, 11:31 PM #13
Re: Help completing my method, IntegerAverage.
So my algorithm, in this case, rounds the wrong way.If you don't understand my response, don't ignore it, ask a question.
 01152016, 11:34 PM #14Member
 Join Date
 Jun 2015
 Posts
 16
 Rep Power
 0
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.
 01152016, 11:51 PM #15Member
 Join Date
 Jun 2015
 Posts
 16
 Rep Power
 0
Re: Help completing my method, IntegerAverage.
Honestly, I have no idea where to go from here.
 01152016, 11:52 PM #16Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,226
 Rep Power
 14
Re: Help completing my method, IntegerAverage.
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,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 01152016, 11:54 PM #17
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.If you don't understand my response, don't ignore it, ask a question.
 01162016, 12:03 AM #18Member
 Join Date
 Jun 2015
 Posts
 16
 Rep Power
 0
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.
 01162016, 12:14 AM #19Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,226
 Rep Power
 14
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,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 01162016, 12:17 AM #20Member
 Join Date
 Jun 2015
 Posts
 16
 Rep Power
 0
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; }
Similar Threads

Completing the method body
By Michael305rodri in forum New To JavaReplies: 16Last Post: 11042012, 06:58 AM 
Need help completing a program to compare two text files!
By nweid1 in forum New To JavaReplies: 6Last Post: 12052011, 07:35 PM 
Need help in completing this code
By nn12 in forum New To JavaReplies: 9Last Post: 09092008, 07:02 AM 
Completing A Program..
By Louise1875 in forum New To JavaReplies: 1Last Post: 05112008, 06:24 AM
Bookmarks