# Beginner Problem: Array sorting decreasing element values? Why?

• February 13th, 2013, 02:24 PM
Hatsman
Beginner Problem: Array sorting decreasing element values? Why?

Code :

```public class GradeExam { public static void main(String[] args) { char [][] answers = { {'A', 'B', 'A', 'C', 'C', 'D', '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', 'E', 'D', 'C', 'E', 'E', 'A', 'D'}, {'A', 'B', 'D', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, {'B', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, {'B', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, {'E', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}};   char[] keys = {'D', 'B', 'D', 'C', 'C', 'D', 'A', 'E', 'A', 'D'};   int[] ArrayF = new int[8]; for (int i = 0; i < answers.length; i++) { int correctCount = 0; for (int j = 0; j < answers.length; j++) { if(answers[i][j] == keys[j] ) correctCount++; } ArrayF[i] = correctCount; } for (int a = 0; a < ArrayF.length - 1; a++) { int currentMin = ArrayF[a]; int currentMinIndex = a; for (int b = a + 1; b < ArrayF.length; b++) { if (currentMin > ArrayF[b]) { currentMin = ArrayF[b]; currentMinIndex = b; } } if (currentMinIndex != a) { ArrayF[currentMinIndex] = ArrayF[a]; ArrayF[a] = currentMin; } } for (int k = 0; k < ArrayF.length; k++) { System.out.println("Student " + (k+1) + "'s correct count was " + ArrayF[k]); } } }```

Bonus question: How would I go about sorting the student number too?
• February 13th, 2013, 02:37 PM
Norm
Re: Beginner Problem: Array sorting decreasing element values? Why?
Quote:

when I call on them to be printed out, they are -2 than what their value should be.
Can you show the print out or other evidence where this is shown?
Post the program's output and explain what is wrong with it and show what it should be.
• February 13th, 2013, 02:54 PM
Hatsman
Re: Beginner Problem: Array sorting decreasing element values? Why?
Sure. Here's some changes I made in order to display what I mean:

Code :

```ArrayF[i] = correctCount;   is now   ArrayF[i] = correctCount; System.out.println("Student " + i + " got " + correctCount);```

also

Code :

```for (int k = 0; k < ArrayF.length; k++) { System.out.println("Student " + (k+1) + "'s correct count was " + ArrayF[k]); }   is now   System.out.println("The correct answers in increasing order are:"); for (int k = 0; k < ArrayF.length; k++) { System.out.println(ArrayF[k]); }```

Doing this, I realized that the problem occurs earlier, when its creating the array in the first place. I hope you do not mind picture evidence:
Apparently, they are put into the array with 2 less than what they should be.
By following the answer key, each student SHOULD have correct answer amounts (in order) as: 7, 6, 5, 4, 8, 7, 7, 7. You can match up the array 'char' with the array 'keys' and see that more should be matching up. What can be causing this?