Results 1 to 19 of 19

Thread: arrayMethod

  1. #1
    Latanyar is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Default arrayMethod

    Does anyone know the formula to getting the median? Here is my program:

    Java Code:
    public class practice2
    {
        public static void main(String[] args)
        {
            Scanner input = new Scanner(System.in);
            int arraysize;  
            double value;
            double [] array; 
    
            double median;
            System.out.println("Please enter the size of the array? (Only numbers greater than 0)");
            arraysize = input.nextInt();
            array = new double [arraysize]; 
            
            for(int i = 0; i < arraysize; i++)
            {
                 
                 System.out.println("Enter the value of the array " + i + ".");
                 value = input.nextDouble();
                 array[i] = value;
            }
            median = median (array);
            System.out.println("Median value is " + median);
        }
        
        public static double median(double[] array)
    
        {
            double value = 0;
            
            
          for(int i =0; i<array.length; i++)
          {
            value += array[i]; 
          }
              return value/array.length;
        }
        
    
            
    }

    It is giving me the average and I need the median.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Yes, the code you posted is for the mean not the median.

    Have a look at any general statistics text for a definition of what a median is. Eg Median - Wikipedia, the free encyclopedia - there is a link on that page to a discussion of how a median might be calculated.

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

  4. #4
    Latanyar is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Default

    This is what I came up with. My median for an odd number seem to work out ok, but when I have even numbers, that is when the problem occur.

    public class practice2
    {
    public static void main(String[] args)
    {
    Scanner input = new Scanner(System.in);
    int arraysize;
    double value;
    double [] array;

    double median;
    System.out.println("Please enter the size of the array? (Only numbers greater than 0)");
    arraysize = input.nextInt();
    array = new double [arraysize];

    for(int i = 0; i < arraysize; i++)
    {

    System.out.println("Enter the value of the array " + i + ".");
    value = input.nextDouble();
    array[i] = value;
    }
    median = median (array);
    System.out.println("Median value is " + median);
    }

    public static double median(double[] array)

    {

    double value = array.length/2;
    int median = 0;

    if(array.length%2 ==1)
    {
    return array[median];
    }
    else
    {
    return (array[median] + array[median]) / 2.0;
    }
    }



    }

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Did you read the Wikipedia link?

    What the median() method is returning is the number the user enters 1/2 way through the input. But, since the user can enter the numbers in any order whatsoever, the number returned may also be anything.

  6. #6
    amro is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    hello
    I am looking for a way to get the median as well
    the median is the value in the middle if n odd and the sum of the tow middle values divided by tow if n is even
    any idea ??

  7. #7
    amro is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    sorry , i forgot to say that the median definition is valid after the data in the array is ordered from smaller to bigger or the other way around
    so lets skip the arranging part
    how to find the median
    thank you in advance
    i am in a great hurry

  8. #8
    amro is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    I found this here : `java array
    i will try it and come back with the result

    public static int middle(int[] user_input)
    {
    int middle = user_input.length/2;
    if (user_input.length%2 == 1)
    {
    return user_input[middle];
    }
    else
    {
    return (user_input[middle-1] + user_input[middle]) / 2;
    }
    }

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by amro View Post
    sorry , i forgot to say that the median definition is valid after the data in the array is ordered from smaller to bigger or the other way around
    so lets skip the arranging part
    That's the fun part of the algorithm; finding the element in the middle (if it exsists) is easy.

    kind regards,

    Jos

  10. #10
    amro is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    its working perfect

  11. #11
    amro is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    i think the fact that indexing starts from zero is a key point especially for those new to programing like my self
    in statistic the middle value must exists if not we calculate it by dividing both values in the middle

    this forum is a great deal and has saved me time even if a problem is not solved one can start from where others approaches ends

    best regards

  12. #12
    amro is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    >>JosAH sorry
    i reported you instead of reply

  13. #13
    amro is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    ok now my statistic calculator have more 2 methods to fill with code namely the variance and the IQR which i think this thread is the please for it
    IQR stands for the Interquartile Range and is defined as

    IQR = upper quartile – lower quartile = Q3 – Q1

    where Q1 is the n/4 th value in the ordered list
    and Q4 is the 3n/4 th value in the ordered list
    any idea ??
    Last edited by amro; 10-31-2010 at 08:08 PM. Reason: formula correction

  14. #14
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    @Amro: you're hijacking Latanyar's thread; please post your question in your own (new) thread. You can of course refer to this thread.

    kind regards,

    Jos

  15. #15
    amro is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    after arranging the data in ascending order and getting the position of Q we must subtract the higher value from the lowest one between which Q stands and multiply the result times the fractional part of the position and add it to the lower value
    if the position is has no fraction there will be no need fore the second part of the storry

    do i need to parse the indexes from int to double before getting the fractions ?
    if yes how?
    I'm using GUI and the array is passed to the method

  16. #16
    amro is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    yes JosAH but i found in the FAQs we should not create a copy of an existence thread
    and its also easier to track
    second Im new here and didnt figure out yet how to open a new thread as i started today (now i knew)
    how to quote someone`s text the way you did in your first reply?? ,I tried to quote and reply you but the result was reporting you

  17. #17
    amro is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    please excuse my bad english

  18. #18
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by amro View Post
    yes JosAH but i found in the FAQs we should not create a copy of an existence thread
    and its also easier to track
    second Im new here and didnt figure out yet how to open a new thread as i started today (now i knew)
    how to quote someone`s text the way you did in your first reply?? ,I tried to quote and reply you but the result was reporting you
    Press the 'quote' button in the lower right corner of the post you want to quote. In the top left corner of the thread listing is a button labeled 'new thread' (or similar); you start a new thread that way.

    kind regards,

    Jos

  19. #19
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

Posting Permissions

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