Reputation: 917
I am iterating an int array to find the median, and then return the median, this portion of the program seems to work.
However, the median has to be cast to a double
and it's showing the decimal place but it's not producing the correct output, why?
import java.util.*;
public class Example
{
public static void main(String[] args)
{
int[] exampleArray = {2, 5, 10, 19, 23, 34};
System.out.println("Median is: " + findMedian(exampleArray));
// The output produced here should be 14.5 not 14.0
}
public static double findMedian(final int[] tempArray)
{
int median = 0,
Index = tempArray.length / 2;
if(tempArray.length % 2 == 1)
{
median = tempArray[Index];
/* I believe the problem is breaking down here I can't cast Index or
the tempArray to a double. I can copy the array elements into a new double array
but I tried that as well and the output was still off.
*/
}
else
median = (tempArray[Index] + tempArray[Index - 1]) / 2;
return (double)median;
}
}
Upvotes: 0
Views: 124
Reputation: 201409
In Java 8+, you might use IntSummaryStatistics
like
System.out.println(IntStream.of(exampleArray).summaryStatistics().getAverage());
In earlier versions of Java, the easiest solution I see would be to sum the elements and then divide the total by the count of elements (which is the median) like
public static double findMedian(final int[] tempArray) {
long total = 0;
for (int val : tempArray) {
total += val;
}
return (double) total / tempArray.length;
}
Upvotes: 0
Reputation: 1468
Your problem is this line of code: median = (tempArray[Index] + tempArray[Index - 1]) / 2;
.
Your median
variable is an int
, and you're trying to assign a double to it, so java is just going to remove the floating part of the expression and give you an int.
This line: return (double)median;
is returning your integer variable (which is the median variable with the floating point removed) as a double, so it's not returning the double, but rather an integer with a floating point (.0).
The solution:
Change int median = 0;
to double median = 0.0;
. This will keep the floating point.
Upvotes: 0
Reputation: 382
Your Index should be int.
double median = 0;
int Index = tempArray.length / 2; //should be int
if(tempArray.length % 2 == 1)
{
median = tempArray[Index];
/* I believe the problem is breaking down here I can't cast Index or
the tempArray to a double. I can copy the array elements into a new double array
but I tried that as well and the output was still off.
*/
}
else
median = (tempArray[Index] + tempArray[Index - 1]) / 2.0;
Upvotes: 0
Reputation: 732
You might change your data type of median to double and ensure you are doing a division of a double value like so:
double median = 0;
Index = tempArray.length / 2;
if(tempArray.length % 2 == 1)
{
median = tempArray[Index];
}
else
{
/* You're having an integer division here. That means it's just
cutting off the digits. A later cast from int to double would do
nothing. There were no digits before.
*/
median = (double)(tempArray[Index] + tempArray[Index - 1]) / 2;
}
return median;
}
Upvotes: 0
Reputation: 83
As you suspected, your issue lies in the line
median = (tempArray[Index] + tempArray[Index - 1]) / 2;
This line is doing integer division. Integer division is what happens when you divide an integer by another integer, and it floors the result, so 5/2 is 2, even though 5.0/2.0 is 2.5.
You need a line like
double preciseMedian = (tempArray[Index] + tempArray[Index - 1]) / 2.0;
The decimal point makes the 2.0 a double, and that makes the entire division happen with the decimal place. Then you need to store it in variable that is a double because you won't be able to put it in an int.
Upvotes: 1
Reputation: 1183
The problem is that you have declared median to be int and performing calculations on int and storing it back to int. Now precision is lost and it is then converted to double which has doesn't regain precision , so change these parts of code.
double median = 0;
median = ((double)tempArray[Index] + (double)tempArray[Index - 1]) / 2;
Upvotes: 0