# arrayMethod

• 10-27-2010, 02:22 AM
Latanyar
arrayMethod
Does anyone know the formula to getting the median? Here is my program:

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.
• 10-27-2010, 02:40 AM
pbrockway2
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.
• 10-27-2010, 02:40 AM
Fubarable
Have a look here: Median - Wikipedia, the free encyclopedia

edit: curses, beaten again!
• 10-27-2010, 05:11 PM
Latanyar
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.

Quote:

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

}
• 10-27-2010, 08:43 PM
pbrockway2

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.
• 10-29-2010, 12:20 PM
amro
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 ??
• 10-29-2010, 12:31 PM
amro
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
i am in a great hurry
• 10-29-2010, 12:46 PM
amro
I found this here : http://www.java-forums.org/new-java/...ava-array.html
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;
}
}
• 10-29-2010, 01:20 PM
JosAH
Quote:

Originally Posted by amro
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-29-2010, 01:35 PM
amro
its working perfect
• 10-29-2010, 01:49 PM
amro
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
• 10-29-2010, 01:53 PM
amro
>>JosAH sorry
• 10-29-2010, 02:13 PM
amro
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 ??
• 10-29-2010, 02:32 PM
JosAH

kind regards,

Jos
• 10-29-2010, 02:46 PM
amro
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
• 10-29-2010, 03:00 PM
amro
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
• 10-29-2010, 03:01 PM
amro
• 10-29-2010, 04:13 PM
JosAH
Quote:

Originally Posted by amro
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
• 10-30-2010, 01:01 AM
al_Marshy_1981
:D this thread is more hijacked than a die hard movie