Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

3. Write a program that contains a static method called median that accepts an a

ID: 3762012 • 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];
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote