Finding the median in an int array

Hi everyone,

I have this program that displays scores from a test. I am trying to write an algorhythm that finds the median score, but mine is not working. Can anyone point me in the right direction? Thanks ahead of time.

import javax.swing.JOptionPane;

import java.util.Arrays;

publicclass Grades{

//Answer Key

public String[] correctAnswer ={"D","B","A","B","C","A","E","E","A","D"};

//Students' Submitted Answers

public String[][] submittedAnswer ={{"D","B","A","B","C","A","E","E","A","D"},

{"D","B","A","B","C","A","E","E","A","D"},

{"E","D","D","A","C","B","E","E","A","D"},

{"C","B","A","A","D","C","E","E","A","D"},

{"A","B","D","E","C","D","E","E","A","D"},

{"B","B","E","B","C","D","E","E","A","D"},

{"B","B","A","B","C","D","E","E","A","D"},

{"E","B","E","E","C","D","E","E","A","D"}};

//Contains the number of correct answers for each student

publicint[] correct =newint[8];

//The user inputs the answer key

publicvoid getAnswer(){

String ans;

String ans2;

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

ans = JOptionPane.showInputDialog("\nPlease enter the correct answer for question " + i +":");

ans2 = ans.toUpperCase();

correctAnswer[i] =ans2;

}

}

//The user inputs student's submitted answers

publicvoid getSubmittedAnswer(){

String ans;

String ans2;

for (int i =0; i><submittedAnswer[0].length; i++){

ans = JOptionPane.showInputDialog("\nPlease enter student answer for question " + i +":");

ans2 = ans.toUpperCase();

submittedAnswer[0][i] =ans2;

}

}

//Compare the student answers to the answer key

publicvoid gradeTest(){

int count = 0;

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

for(int j = 0; j < submittedAnswer[i].length; j++){

if(submittedAnswer[i][j].equals(correctAnswer[j]))

count ++;

}

correct[i] = count;

count=0;

//System.out.println("The no. correct answers for student " + i + " are " + correct[i]);

}

}

//Find the highest test score

publicint findMax(){

if (correct.length > 0){

int max = correct[0];

for (int i = 1; i < correct.length; i++){

if (correct[i] > max)

max = correct[i];

}

System.out.println("The highest test score is: " + max);

return max;

}

else

return 0;

}

//Find the lowest test score

publicint findMin(){

if (correct.length > 0){

int min = correct[0];

for (int i = 1; i < correct.length; i++){

if (correct[i] < min)

min = correct[i];

}

System.out.println("The lowest test score is: " + min);

return min;

}

else

return 0;

}

//Calculates the average test score

publicvoid calcAvg(){

double sum = 0;

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

sum = sum + correct[i];

}

doubleavg = sum / correct.length;

System.out.println("The average score is " + avg);

}

//Calculates the median test score

publicvoid calcMedian(){

Arrays.sort(correct);

double median = correct.length / 2;

System.out.println("The median score is " + median);

}

public Grades(){}

}

[9584 byte] By [jawolkena] at [2007-10-2 20:15:47]
# 1

I think you want the median to be the element of the sorted array at index "correct.length / 2". You are just printing the index itself.

int medianIndex = correct.length/2;

// You should know how to get the associated array value.

double median = ?

MLRona at 2007-7-13 22:58:07 > top of Java-index,Java Essentials,New To Java...
# 2
Sort the list then get the item in the "middle".[edit[Which I see you are doing. How is it wrong? What result are you getting?Message was edited by: BaltimoreJohn
BaltimoreJohna at 2007-7-13 22:58:07 > top of Java-index,Java Essentials,New To Java...
# 3

> Sort the list then get the item in the "middle".

>

> [edit[

> Which I see you are doing. How is it wrong? What result are you getting?

No, as I said in my previous post, that isn't what he's doing. He's printing the index. He needs to print the value at that index.

MLRona at 2007-7-13 22:58:07 > top of Java-index,Java Essentials,New To Java...
# 4
Oops. :-)
BaltimoreJohna at 2007-7-13 22:58:07 > top of Java-index,Java Essentials,New To Java...
# 5
You also might want to think about copying a copy of the array rather than the actual one if you care about the order of the main array.
BaltimoreJohna at 2007-7-13 22:58:07 > top of Java-index,Java Essentials,New To Java...
# 6
Sorting is easy to code, but not efficient. You should consider whether it's worth spending the extra time to implement a linear algorithm for finding the median. You'll find such an algorithm in any good textbook under "Order statistics".
YAT_Archivista at 2007-7-13 22:58:07 > top of Java-index,Java Essentials,New To Java...