1. Member
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.

2. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15
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. Member
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;
}
}

}

4. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15

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.

5. Member
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 ??

6. Member
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
i am in a great hurry

7. Member
Join Date
Oct 2010
Posts
45
Rep Power
0
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;
}
}

8. 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

9. Member
Join Date
Oct 2010
Posts
45
Rep Power
0
its working perfect

10. Member
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

11. Member
Join Date
Oct 2010
Posts
45
Rep Power
0
>>JosAH sorry

12. Member
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; 10-31-2010 at 09:08 PM. Reason: formula correction

kind regards,

Jos

14. Member
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

15. Member
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

16. Member
Join Date
Oct 2010
Posts
45
Rep Power
0

17. 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

18. Senior Member
Join Date
Feb 2010
Location
Waterford, Ireland
Posts
748
Rep Power
8
:D this thread is more hijacked than a die hard movie

#### Posting Permissions

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