# Thread: Calculating Median and mode

1. Member Join Date
Mar 2011
Posts
16
Rep Power
0

## Calculating Median and mode

Trying to calculate median and mode after a user enters 10 numbers from an array.

This is what I have for the mode, but it says the mode is 0.0 everytime :/ :
Java Code:
```double modeOfArray=0;
double maxValue = -1;
int maxCount = 0;
System.out.println ("This is the mode " + modeOfArray);
for (int i = 0; i < numbers.length; i++){
int count = 0;

for(int j = 0; j < numbers.length; j++){
if(numbers[j] == numbers[i]){
count++;
}
}
if (count > maxCount){
maxValue = numbers[i];
maxCount = count;
}
}
}
}```
This is what I have for the median, and it works when the numbers are inputted, but the numbers always don't go from smallest to largest, so they need to be sorted. Any idea how to put sorting into this code for median? please help!

Java Code:
```double median = 0;
double[] nums = new double;

if(number_of_elements % 2 == 0)
{
median = ( numbers[number_of_elements / 2] + numbers[(number_of_elements / 2) + 1] ) / 2;
}
else

median = numbers[((number_of_elements - 1) / 2) + 1];

System.out.println ("The median is " + median);```
:confused:  Reply With Quote

2. ## You can sort code easily with the static method Arrays.sort.

Your mode logic looks pretty good at first glance, what have you done to try and debug it?

Edit: looking at the mode method, you are printing the mode before the loops that find the mode, doesn't that seem strange to you?  Reply With Quote

3. ##  Originally Posted by rochla16 Trying to calculate median and mode after a user enters 10 numbers from an array.

This is what I have for the mode, but it says the mode is 0.0 everytime :/ :
Java Code:
```double modeOfArray=0;
double maxValue = -1;
int maxCount = 0;
System.out.println ("This is the mode " + modeOfArray);
for (int i = 0; i < numbers.length; i++){
int count = 0;

for(int j = 0; j < numbers.length; j++){
if(numbers[j] == numbers[i]){
count++;
}
}
if (count > maxCount){
maxValue = numbers[i];
maxCount = count;
}
}
}
}```

You can't expect modeOfArray to ever change if you don't make it change. Where do you set modeOfArray = to anything other than 0?

This is what I have for the median, and it works when the numbers are inputted, but the numbers always don't go from smallest to largest, so they need to be sorted. Any idea how to put sorting into this code for median? please help!
If you have the numbers in an array, then java.util.Arrays.sort(myArray) should do the trick.  Reply With Quote

4. Member Join Date
Mar 2011
Posts
16
Rep Power
0

## i changed where the print line was, and i added the sort, but the median still isn't calculating right..it seems an integer off or something? and what do you mean by setting modeOfArray to something other than 0? What should it be?

Java Code:
```java.util.Arrays.sort(numbers);
double median = 0;
double[] nums = new double;

if(number_of_elements % 2 == 0)
{
median = ( numbers[number_of_elements / 2] + numbers[(number_of_elements / 2) + 1] ) / 2;
}
else

median = numbers[((number_of_elements - 1) / 2) + 1];

System.out.println ("The median is " + median);

double modeOfArray=0;
double maxValue = -1;
int maxCount = 0;

for (int i = 0; i < numbers.length; i++){
int count = 0;
for(int j = 0; j < numbers.length; j++){
if(numbers[j] == numbers[i])++count;

}
if (count > maxCount){
maxValue = numbers[i];
maxCount = count;
System.out.println ("This is the mode " + modeOfArray);
}
}
}
}```  Reply With Quote

5. ## Two nested for loops for mode? That is unnecessary overhead. You should have 2 variables: mode and modeCount. You should sort the array first, then in the only for loop, check each index against its previous index. If they are equal, add 1 to count, if not, check if the current count is greater than modeCount, if so set modeCount to count and set mode to the current index value. Then set count to 0 :)  Reply With Quote

arrays, median, mode 