programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Help with an Array

Alvaroo Hernandez
Greenhorn
Posts: 24

i am doing an array assignment on Coding Bat and the instructions read "Given an array of integers, return the middle value if the length is odd,
return the average of the two middle values if the length is even, and return zero if the length is zero."

this is the link --> Coding Bat <--

i am having trouble passing 2 tests which are middleValue([1, 6, 5, 4, 7, 2]) → 4.0 and middleValue([]) → 0.0

any help on what to modify to the code to pass these 2 tests to get all green checkmarks?

Alvaroo Hernandez
Greenhorn
Posts: 24

Carey Brown
Saloon Keeper
Posts: 3322
46
How would you  test for an array length of zero?

Alvaroo Hernandez
Greenhorn
Posts: 24
Carey Brown wrote:How would you  test for an array length of zero?

something like if ( a.length == 0)
return 0;    ??

Henry Wong
author
Sheriff
Posts: 23295
125
Alvaroo Hernandez wrote:"Given an array of integers, return the middle value if the length is odd, return the average of the two middle values if the length is even, and return zero if the length is zero."

i am having trouble passing 2 tests which are middleValue([1, 6, 5, 4, 7, 2]) → 4.0 and middleValue([]) → 0.0

Then supposely ... according to the assignment, the "average" of 5 and 4 is 4.0? Did the assignment give any more tests to pass? -- as you probably need to better understand the definition of "average" as defined by the assignment.

Henry

Alvaroo Hernandez
Greenhorn
Posts: 24
Henry Wong wrote:
Alvaroo Hernandez wrote:"Given an array of integers, return the middle value if the length is odd, return the average of the two middle values if the length is even, and return zero if the length is zero."

i am having trouble passing 2 tests which are middleValue([1, 6, 5, 4, 7, 2]) → 4.0 and middleValue([]) → 0.0

Then supposely ... according to the assignment, the "average" of 5 and 4 is 4.0? Did the assignment give any more tests to pass? -- as you probably need to better understand the definition of "average" as defined by the assignment.

Henry

middleValue([9, 8, 6, 9, 9]) → 6.0 6.0 OK
middleValue([2, 5, 2, 4, 3, 5, 2]) → 4.0 4.0 OK
middleValue([1, 6, 5, 4, 7, 2]) → 4.0 4.5 X
middleValue([6]) → 6.0 6.0 OK
middleValue([]) → 0.0 Exception:java.lang.ArrayIndexOutOfBoundsException: 0 (line number:4) X
middleValue([0, -2, -4]) → -2.0 -2.0 OK
middleValue([8, 10]) → 9.0 9.0 OK
middleValue([4, 5, 5]) → 5.0 5.0 OK
other tests
OK

Alvaroo Hernandez
Greenhorn
Posts: 24
Alvaroo Hernandez wrote:
Henry Wong wrote:
Alvaroo Hernandez wrote:"Given an array of integers, return the middle value if the length is odd, return the average of the two middle values if the length is even, and return zero if the length is zero."

i am having trouble passing 2 tests which are middleValue([1, 6, 5, 4, 7, 2]) → 4.0 and middleValue([]) → 0.0

Then supposely ... according to the assignment, the "average" of 5 and 4 is 4.0? Did the assignment give any more tests to pass? -- as you probably need to better understand the definition of "average" as defined by the assignment.

Henry

middleValue([9, 8, 6, 9, 9]) → 6.0 6.0 OK
middleValue([2, 5, 2, 4, 3, 5, 2]) → 4.0 4.0 OK
middleValue([1, 6, 5, 4, 7, 2]) → 4.0 4.5 X
middleValue([6]) → 6.0 6.0 OK
middleValue([]) → 0.0 Exception:java.lang.ArrayIndexOutOfBoundsException: 0 (line number:4) X
middleValue([0, -2, -4]) → -2.0 -2.0 OK
middleValue([8, 10]) → 9.0 9.0 OK
middleValue([4, 5, 5]) → 5.0 5.0 OK
other tests
OK

thats what i dont get the average of 9 / 2 is 4.5 but it says outcome should be 4? maybe switching it to an int??

Henry Wong
author
Sheriff
Posts: 23295
125
Alvaroo Hernandez wrote:
thats what i dont get the average of 9 / 2 is 4.5 but it says outcome should be 4? maybe switching it to an int??

You only have two valid data points... but I guess, from that, and assuming that it is not an error in the assignment, it seems to be the only conclusion that can be drawn.

Henry

Carey Brown
Saloon Keeper
Posts: 3322
46
Alvaroo Hernandez wrote:
Carey Brown wrote:How would you  test for an array length of zero?

something like if ( a.length == 0)
return 0;    ??
yep

Junilu Lacar
Sheriff
Posts: 11490
180
You don't have to do all that casting to (double) - an int will automatically be promoted to double on return.

One line 4, you cast the two array elements you use in the calculation for average to (double). Then you divide the sum of those two numbers by 2 (an int literal). Now, since you have an expression that is (double + double) / int, the divisor will first be promoted to a double value and the result of the expression will already be a double value. So, your calculation that gives 4.5 is actually correct.

Now, if you didn't cast the two values you take from the array, your expression would be just (int + int) / int -- because all terms are int, then no type promotion will be done when calculations are performed. The result of an int division is an int, so (4 + 5) / 2 ==> 4 (int). If this is the value that you return, it will be automatically promoted to double, which will be 4.0 (double). Try it without casting and you'll see your test pass.

I would say that site's test is actually buggy in that it expects the incorrect result of 4.0 instead of the correct result of 4.5 for that case.

Junilu Lacar
Sheriff
Posts: 11490
180

1. Already mentioned this but you are doing way too much casting. Redundant code creates unnecessary clutter and it makes the reader work harder to read your code. It also makes them wonder why the extra code is there. Is there anything significant about the redundant code or did the author just not know any better?

2. Your variable name median is misleading. Based on the test data given, the value you are returning isn't actually the median. A median needs the numbers to be sorted in value order. The test data you are using is not sorted at all. So it's misleading to call the value you return as the "median". Besides, you don't really need that temporary variable since you can directly return it as the result of an expression. I used a variable named mid but it represented the index of the middle element. You don't do that and it makes your code harder to read because of all the calculations you're doing inside the brackets [ .. ]

3. You really should format your code properly so that it's easier to see the logical structure and organization of your code. It's the one easy thing that you can do to help yourself and yet you don't make that small effort to do so.

Junilu Lacar
Sheriff
Posts: 11490
180
Here's what I was talking about making your code cleaner and clearer:
You wrote:

First, fix the formatting:

Now the structure of your logic is clearer. Now get rid of that misleading and unnecessary median variable, remove the extra casts and add braces to your control structures:

Then get rid of the else part and unindent the line of code that was inside it. Also, add a mid variable to get rid of all that clutter in the calculation.

Now, there are some folks who will say "You have two return statements, that's bad!"  If this were a huge method with more than 10 lines of code, I would tend to agree. The concern with multiple return statements in a method is that you can lose track of the lines of execution through your method. In large methods with many lines of code, this is a bad thing.  However, with just a few lines of code in this method, it's not difficult to see where the exit points are. And not having that extraneous else clause actually makes the method shorter. Do you see how much cleaner and clearer your code can be with a little effort?

It's actually easier to see the "feature/bug" on line 4 of this code now because it's clear that we're doing an (int + int) / int calculation. The way to get the real correct result would be to force the divisor to be a double literal (but again, making this correction will actually make your code fail the buggy test on that site).

Of course, this code is still missing the part that needs to handle the case where a.length == 0.