Results 1 to 2 of 2
  1. #1
    leoshiner is offline Member
    Join Date
    Oct 2010
    Rep Power

    Default outputting results from bubble sorted array in correct numerical order

    Hi I have set up this 2D Array to read off of a 1D key and ouput the results,I have then set the program up to sort the results and display
    them in ascending order but I cannot seem to get the sorted results to display the correct student number beside the sorted results.I am thinking
    I might have the variable counter2 set up incorrectly.At the moment when program is run first line after "results in correct order should read:"The student 3 got a count of 4" but instead it reads "The student 7 got a count of 4"

    I would appreciate any help with this,thanks

    Java Code:
    package gradeexam2;
    public class GradeExam2 {
        public static void main(String[] args) {
            char ans[][] = {{'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 key[] =    {'D', 'B', 'D', 'C', 'C', 'D', 'A', 'E', 'A', 'D'};
            System.out.print("Students answers:\n");
            int counter[] = new int[ans.length];
            int sortArray[]=new int[ans.length];
            for (int i = 0; i < ans.length; i++) {
                for (int j = 0; j < ans[i].length; j++) {
                    if (ans[i][j] == key[j]) {
               for (int i = 0; i < ans.length; i++) {
                System.out.println("Student " + i + "'s correct count is " + sortArray[i]);
    System.out.println("results in correct order:");
    //Bubble sort new array
              int counter2=0;
                 for (int x = sortArray.length-1;x>=0;x--){
                     for(int y=0;y<sortArray.length-1;y++){
                         int temp1= sortArray[y];
                         int temp2= sortArray[y+1];
                        if(sortArray[y]<sortArray[y+1]) {
                            //swap values
                   System.out.println("The Student"+counter2+" got a count of "+sortArray[x]);  
    Last edited by Norm; 11-13-2011 at 07:22 PM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power

    Default Re: outputting results from bubble sorted array in correct numerical order

    Sounds like you need to debug your code to find the logic error.
    Have you tried adding printlnl statements to show the logic flow and the values of variables as they change?

Similar Threads

  1. Replies: 1
    Last Post: 11-13-2011, 12:25 PM
  2. Checking to see if Array is sorted
    By pytho in forum New To Java
    Replies: 5
    Last Post: 07-06-2011, 01:39 AM
  3. How to place the GUI components in correct order
    By impact in forum New To Java
    Replies: 2
    Last Post: 05-04-2008, 06:41 AM
  4. Replies: 8
    Last Post: 04-22-2008, 03:21 AM
  5. Replies: 0
    Last Post: 04-14-2008, 08:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts