Results 1 to 19 of 19
Thread: arrayMethod
 10272010, 03:22 AM #1Member
 Join Date
 Oct 2010
 Posts
 25
 Rep Power
 0
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.
 10272010, 03:40 AM #2Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,585
 Rep Power
 12
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.

 10272010, 06:11 PM #4Member
 Join Date
 Oct 2010
 Posts
 25
 Rep Power
 0
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;
}
}
}
 10272010, 09:43 PM #5Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,585
 Rep Power
 12
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.
 10292010, 01:20 PM #6Member
 Join Date
 Oct 2010
 Posts
 45
 Rep Power
 0
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 ??
 10292010, 01:31 PM #7Member
 Join Date
 Oct 2010
 Posts
 45
 Rep Power
 0
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
 10292010, 01:46 PM #8Member
 Join Date
 Oct 2010
 Posts
 45
 Rep Power
 0
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[middle1] + user_input[middle]) / 2;
}
}
 10292010, 02:20 PM #9
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,783
 Blog Entries
 7
 Rep Power
 21
 10292010, 02:35 PM #10Member
 Join Date
 Oct 2010
 Posts
 45
 Rep Power
 0
its working perfect
 10292010, 02:49 PM #11Member
 Join Date
 Oct 2010
 Posts
 45
 Rep Power
 0
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
 10292010, 02:53 PM #12Member
 Join Date
 Oct 2010
 Posts
 45
 Rep Power
 0
>>JosAH sorry
i reported you instead of reply
 10292010, 03:13 PM #13Member
 Join Date
 Oct 2010
 Posts
 45
 Rep Power
 0
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; 10312010 at 09:08 PM. Reason: formula correction
 10292010, 03:32 PM #14
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,783
 Blog Entries
 7
 Rep Power
 21
@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
 10292010, 03:46 PM #15Member
 Join Date
 Oct 2010
 Posts
 45
 Rep Power
 0
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
 10292010, 04:00 PM #16Member
 Join Date
 Oct 2010
 Posts
 45
 Rep Power
 0
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
 10292010, 04:01 PM #17Member
 Join Date
 Oct 2010
 Posts
 45
 Rep Power
 0
please excuse my bad english
 10292010, 05:13 PM #18
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,783
 Blog Entries
 7
 Rep Power
 21
 10302010, 02:01 AM #19Senior Member
 Join Date
 Feb 2010
 Location
 Waterford, Ireland
 Posts
 748
 Rep Power
 5
Bookmarks