Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Sorting a row in a two deminsional array

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've been racking my brain at a particular problem with sorting a certain row of integers in a two dimensional array. I need to sort them in ascending order, as this is part of my final project in my Intro. to Java Programming class.

Ok, the problem is that I have to receive input of 8 students via JOptionPane. There are 6 quizzes that each student receives a grade for, and the numerical assignment is done using Math.random. So the array is,


I'm supposed to write the program so that it can receive user input on which quiz to sort. It's sorted in ascending order, so the names (a seperate array in itself) have to correspondingly change order also.
Example:
Output format:

--------------------------------------
This is a sorted list for test 4
Sach 0
Ami 20
Zack 22
Hum 23
Dum 80
�.
-------------------------------------

My instructor never showed us how to sort arrays with the predefined class that's included in the Java Class Library, but rather wrote a method himself,


As you can see, this is only for single dimensional arrays, so I'm at a loss on how I should modify this code to allow for a two dimensional array. Perhaps I shouldn't declare the 8 names in their own array?

This is about as far as I could get to, even after looking on the internet for a bit. Also, I DO know that there's a compile problem with this, but the only way I can solve it is to set the arrays in the swap method as one-dimensional.


I apologize for the lengthy post, but I'm trying to be as specific as possible here. There's no rule on HOW I sort the array, just as long as I can get the program to output the form of what was in the example.
[ November 26, 2005: Message edited by: John Oxford ]
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public static void sorting(String[] name, int[][] score)
{
String temp;
int tempint=0;
temp = JOptionPane.showInputDialog("Which test would you like sorted?"+
"\nnote: Your entry should be one integer less than the test you want!"+
"\nexample: The test you want is 5; entry = 4");
tempint = Integer.parseInt(temp);

for(int j=score.length; j>=2; j--)
for(int k=0; k<j-1; k++)
if(score[tempint][k]>score[tempint][k+1])
swap(score,tempint, k, k+1);
for(int i=0; i<8; i++)
System.out.println(score[tempint][i]+"\n");
}



public static void swap(int[][] score,int x,int pos1, int pos2)
{
int temp;
temp = score[x][pos1];
score[x][pos1] = score[x][pos2];
score[x][pos2] = temp;
}
 
reply
    Bookmark Topic Watch Topic
  • New Topic