Results 1 to 3 of 3
  1. #1
    leoshiner is offline Member
    Join Date
    Oct 2010
    Posts
    28
    Rep Power
    0

    Default sorting an array and then displaying in descending order

    Hi I am writing a program that will crosscheck a 2D array off a 1D array key and output the results
    I have managed to output the results and now need to output them in order starting with the highest mark first
    I have been partially successful in this but it will not display Student number 0 in the results.I would appreciate any
    help on 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]) {
                        counter[i]++;
                    }
                   
                }
                
                 sortArray[i]=counter[i];
                 
            }
               for (int i = 0; i < ans.length; i++) {
    
    
                System.out.println("Student " + i + "'s correct count is " + counter[i]);
            }
            //Bubble sort new array
                 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
                            sortArray[y]=temp2;
                            sortArray[y+1]=temp1;
                        }
                     }
                   System.out.println("Student number "+x+" got a count of "+sortArray[x]);  
                 }
                 
                     
                 
                 
                    
                 
            }
         
    
    
    
    
    
        }
    Last edited by Fubarable; 11-13-2011 at 04:44 PM. Reason: code tags added

  2. #2
    eRaaaa is online now Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default Re: sorting an array and then displaying in descending order

    for (int x = sortArray.length-1;x>0;x--){
    -->
    for (int x = sortArray.length - 1; x >= 0; x--) {


    (but your results are wrong or? you are mixed some wrong together?!)

  3. #3
    leoshiner is offline Member
    Join Date
    Oct 2010
    Posts
    28
    Rep Power
    0

    Default Re: sorting an array and then displaying in descending order

    Hi just worked out that I should have x>=0 to get correct results now I just need to work out how to get it to output which number student got which results,i.e. first output
    when sorted should read

    Student number 4 got a count of 8 etc etc.,I supposed in order to do this I'll have to create another variable to hold the positions or something,thanks

Similar Threads

  1. Sorting in ascending and descending order
    By flpanthers1 in forum New To Java
    Replies: 10
    Last Post: 06-27-2011, 03:48 PM
  2. Replies: 3
    Last Post: 01-13-2011, 06:07 PM
  3. [SOLVED] Sorting array in descending order?
    By dan0 in forum New To Java
    Replies: 14
    Last Post: 04-16-2009, 12:19 AM
  4. Sorting in descending order
    By santanu in forum New To Java
    Replies: 6
    Last Post: 11-26-2008, 11:43 PM
  5. Descending order
    By santanu in forum New To Java
    Replies: 1
    Last Post: 11-04-2008, 04:33 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
  •