Results 1 to 3 of 3
- 11-13-2011, 04:41 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 28
- Rep Power
- 0
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
- 11-13-2011, 05:05 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
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--) {
.gif)
(but your results are wrong or? you are mixed some wrong together?!)
- 11-13-2011, 05:17 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 28
- Rep Power
- 0
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
-
Sorting in ascending and descending order
By flpanthers1 in forum New To JavaReplies: 10Last Post: 06-27-2011, 03:48 PM -
Arrange the given numbers of the array in descending order
By radhi16 in forum New To JavaReplies: 3Last Post: 01-13-2011, 06:07 PM -
[SOLVED] Sorting array in descending order?
By dan0 in forum New To JavaReplies: 14Last Post: 04-16-2009, 12:19 AM -
Sorting in descending order
By santanu in forum New To JavaReplies: 6Last Post: 11-26-2008, 11:43 PM -
Descending order
By santanu in forum New To JavaReplies: 1Last Post: 11-04-2008, 04:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks