# Calculating Median and mode

• 04-29-2011, 03:04 AM
rochla16
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 :/ :
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!

Code:

```double median = 0; double[] nums = new double[10];   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:
• 04-29-2011, 03:24 AM
sunde887
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?
• 04-29-2011, 03:26 AM
Fubarable
Quote:

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 :/ :
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?

Quote:

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.
• 04-29-2011, 03:39 AM
rochla16
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?

Code:

```java.util.Arrays.sort(numbers); double median = 0; double[] nums = new double[10];   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);             }         }     } }```
• 04-29-2011, 04:24 AM
ra4king
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 :)