# Thread: Help completing my method, IntegerAverage.

1. Member 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;
}
}```
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.  Reply With Quote

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.

[code]
[/code]
to get highlighting and preserve formatting.  Reply With Quote

3. Member 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.  Reply With Quote

4. ## 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?
Last edited by Norm; 01-15-2016 at 06:31 PM.  Reply With Quote

5. Member 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.  Reply With Quote

6. Member 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.  Reply With Quote

7. ## 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.  Reply With Quote

8. Member 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; 01-15-2016 at 08:16 PM.  Reply With Quote

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
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?  Reply With Quote

10. Member 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?  Reply With Quote

11. ## 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.  Reply With Quote

12. Member 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.  Reply With Quote

13. ## Re: Help completing my method, IntegerAverage.

So my algorithm, in this case, rounds the wrong way.
Ok, time to find an algorithm that works.  Reply With Quote

14. Member 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.  Reply With Quote

15. Member 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.  Reply With Quote

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

## Re: Help completing my method, IntegerAverage. Originally Posted by mhamill2 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  Reply With Quote

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.  Reply With Quote

18. Member 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.  Reply With Quote

19. Senior 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,
Jim  Reply With Quote

20. Member 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;
}```  Reply With Quote

#### Posting Permissions

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