Results 1 to 5 of 5
  1. #1
    rochla16 is offline Member
    Join Date
    Mar 2011
    Posts
    16
    Rep Power
    0

    Question 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[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:

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by rochla16 View Post
    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.

  4. #4
    rochla16 is offline Member
    Join Date
    Mar 2011
    Posts
    16
    Rep Power
    0

    Default

    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);
                }
            }
        }
    }

  5. #5
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    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

  1. Debug mode
    By Aaron_Sharp in forum New To Java
    Replies: 3
    Last Post: 01-10-2011, 04:44 AM
  2. wrapped mode
    By bigj in forum NetBeans
    Replies: 1
    Last Post: 07-19-2010, 10:22 PM
  3. Replies: 0
    Last Post: 03-18-2010, 06:39 PM
  4. Finding Median of X Integers
    By Hasan in forum New To Java
    Replies: 3
    Last Post: 08-12-2008, 03:06 PM
  5. Quick sort with median-of-three partitioning
    By Java Tip in forum Algorithms
    Replies: 0
    Last Post: 04-15-2008, 08:40 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •