Results 1 to 2 of 2
- 11-13-2011, 06:06 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 28
- Rep Power
- 0
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]) { counter[i]++; } } sortArray[i]=counter[i]; } for (int i = 0; i < ans.length; i++) { System.out.println("Student " + i + "'s correct count is " + sortArray[i]); } System.out.println(); 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 sortArray[y]=temp2; sortArray[y+1]=temp1; } } counter2=x; System.out.println("The Student"+counter2+" got a count of "+sortArray[x]); } } }Last edited by Norm; 11-13-2011 at 06:22 PM. Reason: added code tags
- 11-13-2011, 06:23 PM #2
Similar Threads
-
checking a 2D Array off a 1D Array and outputting result
By leoshiner in forum New To JavaReplies: 1Last Post: 11-13-2011, 11:25 AM -
Checking to see if Array is sorted
By pytho in forum New To JavaReplies: 5Last Post: 07-06-2011, 01:39 AM -
How to place the GUI components in correct order
By impact in forum New To JavaReplies: 2Last Post: 05-04-2008, 06:41 AM -
how to right a program that find kth number in two sorted array?
By fireball2008 in forum New To JavaReplies: 8Last Post: 04-22-2008, 03:21 AM -
Sorting, Searching, and Inserting into a sorted array
By Java Tip in forum java.langReplies: 0Last Post: 04-14-2008, 08:39 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks