Results 1 to 5 of 5
Thread: Calculating Median and mode
 04292011, 04:04 AM #1Member
 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; } } } }
Java 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);
 04292011, 04:24 AM #2
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 12
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?

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!
 04292011, 04:39 AM #4Member
 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[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); } } } }
 04292011, 05:24 AM #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 :)
Similar Threads

Debug mode
By Aaron_Sharp in forum New To JavaReplies: 3Last Post: 01102011, 04:44 AM 
wrapped mode
By bigj in forum NetBeansReplies: 1Last Post: 07192010, 10:22 PM 
gwt 2.0 hosted mode "plugin failed to connect to hosted mode server"
By turanan in forum New To JavaReplies: 0Last Post: 03182010, 06:39 PM 
Finding Median of X Integers
By Hasan in forum New To JavaReplies: 3Last Post: 08122008, 03:06 PM 
Quick sort with medianofthree partitioning
By Java Tip in forum AlgorithmsReplies: 0Last Post: 04152008, 08:40 PM
Bookmarks