3. Write a program that contains a static method called median that accepts an a
ID: 3762072 • Letter: 3
Question
3. Write a program that contains a static method called median that accepts an array of integers as its argument and returns the median of the numbers in the array. The median is the number that will appear in the middle if you arrange the elements in order. If there is an even number of values, then the median is then the mean of the two middle values, in the main method, the program prompts the user to first enter the total number of values, then asks the user to enter all values, and concludes by calling the median method and displaying the result.Explanation / Answer
public static void bubbleSort(int a[],int size){
int temp;
for(int i = 0; i < size; i++){
for(int j = i+1; j < size; j++){
if(a[j] > a[i]){
temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
}
}
public static int median(int a[],int size){
bubbleSort(a,size);
if(size % 2 == 0)
return a[size/2 - 1] + a[size/2 + 1]/2;
else
return a[size/2];
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.