i have to calculate the MEDIAN of an unsorted array(i.e,array must be in sorted order before calculation of median).I am a beginner in java.kindly help me with some kinda code,and learn me how to calculate the median in this case?thankx

2
Contributors
8
Replies
20
Views
4 Years
Discussion Span
Last Post by JamesCherrill

Read the API doc for Arrays class, sort() method. We don't write people's homework for them, but make an effort and someone will help.

well em also doing effort..and first time i joined such kinda forum...em not asking for FULL help,actually i already had taken an input of 25 arrays and then found it's median successfully,now em facing problem in the actual LOGIC behind finding the median,at that time,when my 25 integers array is not sorted!!that's it sir!thnkx

em looking forward for your,as well as other peoples' reply...

As far as I know you have to sort the numbers then take the middle one.
The Arrays class has sort methods to make this really easy for you - see the API doc

yeah exaccctly!!! i wanna do such thing in my code...:) well are yew talking about this one ... http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#binarySearch%28short[],%20short%29 ???? nd em unable to sort out my problem from this......actually,em unable to understand the logic as well as different codes...as their are may sorting algorithms ....kindly guide me thanks ! i have already calculated the mean and also made the menu for calling these three things,nd nooow i have to calculate the mode and median

``````import java.util.Scanner;
class mean
{
public static void main(String[] args)
{

int c=0;
int m[]=new int[15];
Scanner input=new Scanner(System.in);
System.out.println("\t\t--------------------------------------------------");
System.out.println("\t\t^_^    Taking an array of fifteen integers   ^_^");
System.out.println("\t\t--------------------------------------------------");
System.out.println("\t\t********* WE HAVE TO CALCULATE THE MEAN *********");
int sum=0;

for(int i=0;i<15;i++)
{
m[i]=input.nextInt();
sum=sum+m[i];
c=(sum/15);
}
System.out.println("meam"+c);
}
}
``````

now i havedone this so far...it not gemme the errors,but it also dun gemme the output,it only display...taking an array,and we hv to calculate the median,now yew have to help me plzz..

``````//This method firstly sort the array and returns middle element if the length of arrray is odd.Otherwise it will return the average of two miidele elements.

class Median
{
public static void main(String args[])
{
int c=0;
int array[]=new int[15];
System.out.println("\t\t--------------------------------------------------");
System.out.println("\t\t^_^    Taking an array of fifteen integers   ^_^");
System.out.println("\t\t--------------------------------------------------");
System.out.println("\t\t********* WE HAVE TO CALCULATE THE MEDIAN *********");

int length=array.length;

int[] sort = new int[length];

System.arraycopy(array, 0, sort, 0, sort.length);

//median.sort(sort);

if (length % 2 == 0)
{
c= (sort[(sort.length / 2) - 1] + sort[sort.length / 2]) / 2;
}
else
{
c= sort[sort.length / 2];
}
}
}
``````

mean program is running,but median is not..help me plz within 24 hours,thanks

All you need is to sort your copy of the array at line 20, ie
Arrays.sort(sort);

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.