Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Java 2D sorting array with sums in output

807580Mar 21 2010 — edited Mar 23 2010
Trying to write a code that creates a 5 x 5 2D array with random numbers from 0-9. Then sorts array columns from lowest to highest top to bottom adding the rows to output a total. Similar to this but with random numbers.

Input array
5 5 5 5 5
4 4 4 4 4
3 3 3 3 3
2 2 2 2 2
1 1 1 1 1

Output array
1 1 1 1 1 = 5
2 2 2 2 2 = 10
3 3 3 3 3 = 15
4 4 4 4 4 = 20
5 5 5 5 5 = 25

All I have so far is the below 2D 5 x 5 array. Stuck on the sorting and the summing of the rows. Any help would be appreciated.

--------------------------------------------------------------
publicclass SortArray {
publicstaticvoid main(String[] args) {

System.out.print("Input Array:");
System.out.println();

int row, col;
int[][] random; // array declaration
random = newint[5][5]; // creation of 5 x 5 array

// fills a 5 x 5 array with random numbers between 0 and 9
for (row = 0; row < scores.length; row++){
for (col = 0; col < scores[row].length; col++){
scores[row][col] = (int)(Math.random() * 9);
}
}

// prints the contents of the array
for (row = 0; row < scores.length; row++){
for (col = 0; col < scores[row].length; col++){
System.out.print( scores[row][col] + " ");
}
System.out.println(); // moves to next line
}
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 20 2010
Added on Mar 21 2010
17 comments
362 views