# Trouble returning the middle of the three numbers

• (2 Pages)
• 1
• 2

## 16 Replies - 6082 Views - Last Post: 10 March 2012 - 08:18 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=269111&amp;s=a596c2f30f4056b5a7f200ca3ceb94e0&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ola3

Reputation: 0
• Posts: 21
• Joined: 17-February 12

# Trouble returning the middle of the three numbers

Posted 02 March 2012 - 07:42 AM

```public class TestMedian{
public static void main(String[] args){
System.out.println(median (7,3,9));
System.out.println(median (29,-14,11));
}
public static int median(int x, int y, int z){
int middle=0;
int middle2=0;
middle= Math.max(x,y);
middle2=Math.max(middle,z);
if (middle>z){
}
return middle;
}
}
```

Is This A Good Question/Topic? 0

## Replies To: Trouble returning the middle of the three numbers

### #2 tlhIn`toq

• Freelance developer

Reputation: 6055
• Posts: 13,326
• Joined: 02-June 10

## Re: Trouble returning the middle of the three numbers

Posted 02 March 2012 - 07:46 AM

Could you be even MORE vague, just to make it interesting?

Look, the harder you make it for us to help you the less likely someone will - That's just human nature and common sense.

How about telling us what the trouble is, what the problem is, ASKING A QUESTION can do wonders.

### #3 ola3

Reputation: 0
• Posts: 21
• Joined: 17-February 12

## Re: Trouble returning the middle of the three numbers

Posted 02 March 2012 - 07:52 AM

The three numbers are passed into the method, what im having trouble with is returning the middle of the three numbers, for example lets say 5,59, 16 are passed in, the number 16 would be returned because its in the middle

tlhIn`toq, on 02 March 2012 - 07:46 AM, said:

Could you be even MORE vague, just to make it interesting?

Look, the harder you make it for us to help you the less likely someone will - That's just human nature and common sense.

How about telling us what the trouble is, what the problem is, ASKING A QUESTION can do wonders.

### #4 SwiftStriker00

• No idea why my code works

Reputation: 438
• Posts: 1,611
• Joined: 25-December 08

## Re: Trouble returning the middle of the three numbers

Posted 02 March 2012 - 07:56 AM

His problem is that System.out.println(median (29,-14,11)); is returning 29.

The code you posted middle2 will always be your largest value, so you would need to convey simlilar logic to this:

```int low, middle;
if( x == Math.max(x,y) ){
middle = x;
low = y;
}else{
middle = y;
low = x;
}

int high, trueMiddle

if( z == Math.max(z,middle ) ){
high= z;
trueMiddle= middle;
}else{
high= middle;
trueMiddle= z;
}
```

### #5 g00se

• D.I.C Lover

Reputation: 3040
• Posts: 12,967
• Joined: 20-September 08

## Re: Trouble returning the middle of the three numbers

Posted 02 March 2012 - 08:00 AM

Math.max is messy for anything other than two numbers. Do yourself a favour - use an array, sort it and return array[1]

### #6 SwiftStriker00

• No idea why my code works

Reputation: 438
• Posts: 1,611
• Joined: 25-December 08

## Re: Trouble returning the middle of the three numbers

Posted 02 March 2012 - 08:09 AM

or you can make your prof cry by trying to read this

```public static int median(int x, int y, int z){
return ( x > y ) ? ( x > z) ? z : x : ( y > z ) ? z : y ;
}

```

### #7 tlhIn`toq

• Freelance developer

Reputation: 6055
• Posts: 13,326
• Joined: 02-June 10

## Re: Trouble returning the middle of the three numbers

Posted 02 March 2012 - 08:55 AM

g00se, on 02 March 2012 - 09:00 AM, said:

Math.max is messy for anything other than two numbers. Do yourself a favour - use an array, sort it and return array[1]

Assuming it is always an array of three elements: 0, 1, 2 - thus [1] is the middle

But a more dynamic approach would be to take the count of the elements (array.Length in this language I think), and divide that in half to get the element closest to the middle.

If you have 27 elements, half of that (as an int to drop the fraction) would be 13 or 14 depending on your systems choice of rounding up or down. Either one could be considered 'the middle'

### #8 g00se

• D.I.C Lover

Reputation: 3040
• Posts: 12,967
• Joined: 20-September 08

## Re: Trouble returning the middle of the three numbers

Posted 02 March 2012 - 09:41 AM

Quote

Assuming it is always an array of three elements: 0, 1, 2 - thus [1] is the middle

Well the method signature given for the assignment would have to be changed to accommodate anything other than that. But the general point is correct - a general solution is usually better

### #9 Dorito_125

Reputation: 8
• Posts: 88
• Joined: 13-December 08

## Re: Trouble returning the middle of the three numbers

Posted 02 March 2012 - 09:55 AM

If I were you I would do away with Math.max and just write it yourself.

Start in Psuedocode.

We know the median is the number that is in between the other two values? SO think to yourself procedurally how you would find that.

```If(x <= y && y <= z)
// We know Y is in the middle?
```

We did this exact same thing in school. If you don't mind me asking what school do you go to? What book do you use?

Shot in the dark, Java Programming from the ground up, Methods Ch.6 Problem 12?

### #10 ola3

Reputation: 0
• Posts: 21
• Joined: 17-February 12

## Re: Trouble returning the middle of the three numbers

Posted 02 March 2012 - 04:31 PM

could you explain this code?

SwiftStriker00, on 02 March 2012 - 08:09 AM, said:

or you can make your prof cry by trying to read this

```public static int median(int x, int y, int z){
return ( x > y ) ? ( x > z) ? z : x : ( y > z ) ? z : y ;
}

```

### #11 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: Trouble returning the middle of the three numbers

Posted 02 March 2012 - 04:37 PM

```  public static int median(int x, int y, int z){
int middle=0;
int middle2=0;
middle= Math.max(x,y);
middle2=Math.max(middle,z);
if (middle>z){
// you do nothing here... probably return z; ?
}
return middle;
}
}

```

### #12 burakaltr

• D.I.C Regular

Reputation: 91
• Posts: 280
• Joined: 07-November 10

## Re: Trouble returning the middle of the three numbers

Posted 02 March 2012 - 07:20 PM

if (x<=y && x>=z) x is in the middle
if (y<=x && y>=z) y is in the middle
if (z<=y && z>=x) z is in the middle
if (x>=y && x<=z) x is in the middle
if (y>=x && y<=z) y is in the middle
if (z>=y && z<=x) z is in the middle

### #13 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: Trouble returning the middle of the three numbers

Posted 02 March 2012 - 07:45 PM

Too trivial. I guess we want a solution that will be also good for N panels

### #14 ola3

Reputation: 0
• Posts: 21
• Joined: 17-February 12

## Re: Trouble returning the middle of the three numbers

Posted 10 March 2012 - 06:39 PM

Could you explain this code in further detail please?

SwiftStriker00, on 02 March 2012 - 08:09 AM, said:

or you can make your prof cry by trying to read this

```public static int median(int x, int y, int z){
return ( x > y ) ? ( x > z) ? z : x : ( y > z ) ? z : y ;
}

```

### #15 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: Trouble returning the middle of the three numbers

Posted 10 March 2012 - 06:48 PM

Same homework ?
All solved there

http://www.dreaminco...8&#entry1572438