This is my code so far. My last thing I need and don't know how because my teacher logged off is to print the median number. The double med = median(nums); needs to be printed and I need help. It should go in the printArray method and its not printing. I have tried many different ways please help. Its due in less than 2 hours.
import java.util.Scanner;
import java.io.*;
public class ArrayLab {
public static void main(String[] args) throws IOException {
double[] nums = loadArray();
printArray(nums);
sort(nums);
printArray(nums);
double med = median(nums);
}
public static double[] loadArray() throws IOException {
Scanner fin = new Scanner(new File("run.txt"));
int size = fin.nextInt();
double[] values = new double[size];
for(int index = 0; index < size; index++)
values[index] = fin.nextDouble();
return values;
}
public static void printArray(double[] values){
for(int index = 0; index < values.length; index++)
System.out.print(values[index] +"\t\t" );
System.out.println();
}//end printArray
public static void sort(double[] list){
for(int pass = 0; pass < list.length-1; pass++) {
double max = list[pass];
int maxIndex = pass;
for(int index = pass+1; index < list.length; index++){
if(max < list[index]){
max = list[index];
maxIndex = index;
}//end of if
}//end of for loop
if(maxIndex != pass) {
list[maxIndex] = list[pass];
list[pass] = max;
}//end of if minIndex
}//end of outer for
}//end method
public static double median(double[] values) {
double median;
if(values.length % 2 == 0)
median = (values[values.length/2] + values[values.length/2 -1])/2.0;
else
median = values[values.length/2];
return median;
}
}//end of public class