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

Find the Median Write a program that randomly populates an array of size 100, so

ID: 3590117 • Letter: F

Question

Find the Median Write a program that randomly populates an array of size 100, sorts it, and then finds the median. The random numbers must be from 0-99 and all integer values. Also since there is an even set of numbers the median is found by taking the mean of the two middle numbers (In other words the 50th and 51st). You have to code your own sorting method. You may not use a built in java sorter. Keep in mind the 50th and 51st correspond to the number of items not necessarily the index of the array. Comment your code Example Dialog: Random array 13 80 93 90 46 56 97 88 81 14 23 99 91 8 95 80 86 53 73 38 93 9 95 8 35 49 74 70 8 50 48 2 72 96 17 75 10 10 55 33 39 37 92 12 68 99 98 94 41 43 5 61 71 95 81 89 8 92 8 68 60 14 36 99 36 44 90 45 99 28 22 80 68 37 51 63 63 81 79 24 33 62 8 2 88 61 79 59 48 7 37 57 7 7 36 39 23 48 32 39 Sorted array 2 2 5 7 7 7 8 8 8 8 8 8 9 10 10 12 13 14 14 17 22 23 23 24 28 32 33 33 35 36 36 36 37 37 37 38 39 39 39 41 43 44 45 46 48 48 48 49 50 51 53 55 56 57 59 60 61 61 62 63 63 68 68 68 70 71 72 73 74 75 79 79 80 80 80 81 81 81 86 88 88 89 90 90 91 92 92 93 93 94 95 95 95 96 97 98 99 99 99 99 Median is 52

Explanation / Answer

MedianTest.java

import java.util.Random;

public class MedianTest {

public static void main(String[] args) {

int a[] = new int[100];

Random r = new Random();

for(int i=0;i<a.length;i++) {

a[i]=r.nextInt(100);

}

System.out.println("Random array: ");

for(int i=0;i<a.length;i++) {

System.out.print(a[i]+" ");

}

System.out.println();

buubleSort(a);

System.out.println("Sorted array: ");

for(int i=0;i<a.length;i++) {

System.out.print(a[i]+" ");

}

System.out.println();

double median = (a[49] + a[50])/2.0;

System.out.println("Median: "+median);

}

public static void buubleSort(int[] a) {

int n = a.length;

int temp = 0;

for(int i=0; i < n; i++){

for(int j=1; j < (n-i); j++){

if(a[j-1] > a[j]){

//swap the elements!

temp = a[j-1];

a[j-1] = a[j];

a[j] = temp;

}

}

}

}

}

Output:

Random array:
1 31 80 41 6 87 47 83 68 14 21 87 97 46 9 94 13 10 49 99 41 34 84 57 66 81 88 88 80 22 89 33 82 6 16 71 63 90 6 19 28 4 29 51 0 38 73 79 33 30 49 56 20 97 5 83 69 69 9 2 98 75 31 74 98 65 97 29 18 92 2 38 14 6 51 24 79 91 4 27 54 10 16 15 97 63 74 24 65 13 9 98 2 6 51 55 24 90 24 34
Sorted array:
0 1 2 2 2 4 4 5 6 6 6 6 6 9 9 9 10 10 13 13 14 14 15 16 16 18 19 20 21 22 24 24 24 24 27 28 29 29 30 31 31 33 33 34 34 38 38 41 41 46 47 49 49 51 51 51 54 55 56 57 63 63 65 65 66 68 69 69 71 73 74 74 75 79 79 80 80 81 82 83 83 84 87 87 88 88 89 90 90 91 92 94 97 97 97 97 98 98 98 99
Median: 46.5

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