1 2 Previous Next 17 Replies Latest reply: Mar 22, 2010 6:53 PM by 699554 RSS

    Java 2D sorting array with sums in output

    807580
      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
      }
      }
      }
        • 1. Re: Java 2D sorting array with sums in output
          807580
          Write a comparator that can order rows against each other.

          Actually the way I'd do this would be to create a Row object and make it internally produce the sum of its elements, and also make it implement Comparable based on that sum.

          But you could also do it with a Comparator.
          • 2. Re: Java 2D sorting array with sums in output
            807580
            By the way, when you post code, wrap it in code tags so it's legible.
            In the text input box when you post, highlight the code and click the "CODE" button above the box.
            System.out.println("Like this.  Easier to read, right?");
            • 3. Re: Java 2D sorting array with sums in output
              807580
              Oh, and use java.util.Arrays.sort to do the actual sorting.

              Edited by: paulcw on Mar 21, 2010 5:58 PM
              • 4. Re: Java 2D sorting array with sums in output
                807580
                Paul,

                Thanks, but like I said, I am lost man. Can you point me in the right direction. With Java I seem to learn best by reverse engineering. Is there a program similar to mine that I can look at that will help me see it on screen so I can better understand how to solve my problem? I don't want anyone to do my work for me, I just have to see it.
                • 5. Re: Java 2D sorting array with sums in output
                  807580
                  import java.util.Arrays;
                  import java.util.Comparator;
                  
                  public class ShowSort {
                    public static void main(String[] argv) {
                      String[] animals = new String[] { "aardvark", "horse", "dog", "cat", "monkey" };
                      System.out.println(Arrays.toString(animals));
                  
                      Arrays.sort(animals);
                      System.out.println(Arrays.toString(animals));
                  
                      Comparator<String> comp = new Comparator<String>() {
                        public int compare(String s1, String s2) {
                       return s1.length() - s2.length();
                        }
                      };
                      Arrays.sort(animals, comp);
                      System.out.println(Arrays.toString(animals));
                  
                    }
                  }
                  Plus I'm sure you can find tutorials on Comparators via Google.

                  The only tricky bit is that your Comparator would compare whole arrays, and not elements in the inner array. It would be straightforward to do but a little bizarre to think about.
                  • 6. Re: Java 2D sorting array with sums in output
                    807580
                    Paul,

                    Ok. I was able to get it sorted and reprinted. What is my next course of action with regards to adding the rows?


                    import java.util.Random;
                    public class SortArray {

                         public static void main(String[] args) {
                         System.out.print("Input Array:");
                         System.out.println();
                         int row, col; int[][] scores;
                         // array declaration
                         scores = new int[5][5];
                         // creation of 5 x 5 array
                         Random random = new Random();
                         // 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] = random.nextInt(10);
                         //0-9 , 9 included
                         }
                         }
                         // 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
                         }
                         System.out.println("Output Array");
                         for (col=0;col< scores[0].length; col++){
                         for (int i=0;i<scores.length;i++)
                         for (int j=i;j<scores.length;j++){
                         if (scores[i][col]>scores[j][col]){
                         int temp=scores[i][col]; scores[i][col]=scores[j][col]; scores[j][col]=temp; }
                         }
                    }
                         // 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
                         }
                         }
                         }
                    • 7. Re: Java 2D sorting array with sums in output
                      807580
                      It's really hard to read that. Can you edit your post, highlight the code part, and then click the CODE button, then save it?
                      • 8. Re: Java 2D sorting array with sums in output
                        807580
                        Paul,

                        Sorry I did it wrong I though you bookended your code with the code utton. I have come a little further. If you look, I was able to add the first row, but it just doubles for the other rows instead of adding. Take a look.
                        import java.util.Random;
                        public class SortArray { 
                        
                             public static void main(String[] args) { 
                             System.out.print("Input Array:"); 
                             System.out.println(); 
                             int row, col; int[][] rrnums; 
                             // array declaration
                             rrnums = new int[5][5]; 
                             // creation of 5 x 5 array
                              Random random = new Random(); 
                             // fills a 5 x 5 array with rrnums numbers between 0 and 9
                              for (row = 0; row < rrnums.length; row++){ 
                              for (col = 0; col < rrnums[row].length; col++){ 
                              rrnums[row][col] = random.nextInt(10);
                              //0-9 , 9 included
                               } 
                               }
                              // prints the contents of the array
                               for (row = 0; row < rrnums.length; row++){
                               for (col = 0; col < rrnums[row].length; col++){ 
                               System.out.print( rrnums[row][col] + " "); } 
                               System.out.println(); 
                               // moves to next line
                                } 
                               System.out.println();
                               System.out.println("Output Array"); 
                               for (col=0;col< rrnums[0].length; col++){ 
                               for (int i=0;i<rrnums.length;i++)
                               for (int j=i;j<rrnums.length;j++){ 
                               if (rrnums[i][col]>rrnums[j][col]){ 
                               int temp=rrnums[i][col];
                               rrnums[i][col]=rrnums[j][col]; 
                               rrnums[j][col]=temp; } 
                               } 
                        } 
                               // prints the contents of the array
                               int maxRow = 0;
                               for (row = 0; row < rrnums.length; row++){ 
                               for (col = 0; col < rrnums[row].length; col++){ 
                               maxRow += rrnums[0][col];
                               System.out.print( rrnums[row][col] + "  =  "); } 
                               System.out.print( maxRow );
                               System.out.println(); 
                               // moves to next line 
                               } 
                               } 
                        }
                        • 9. Re: Java 2D sorting array with sums in output
                          807580
                          Update,

                          Had the = in the wrong spot.
                          import java.util.Random;
                          public class SortArray { 
                          
                               public static void main(String[] args) { 
                               System.out.print("Input Array:"); 
                               System.out.println(); 
                               int row, col; int[][] rrnums; 
                               // array declaration
                               rrnums = new int[5][5]; 
                               // creation of 5 x 5 array
                                Random random = new Random(); 
                               // fills a 5 x 5 array with rrnums numbers between 0 and 9
                                for (row = 0; row < rrnums.length; row++){ 
                                for (col = 0; col < rrnums[row].length; col++){ 
                                rrnums[row][col] = random.nextInt(10);
                                //0-9 , 9 included
                                 } 
                                 }
                                // prints the contents of the array
                                 for (row = 0; row < rrnums.length; row++){
                                 for (col = 0; col < rrnums[row].length; col++){ 
                                 System.out.print( rrnums[row][col] + " "); } 
                                 System.out.println(); 
                                 // moves to next line
                                  } 
                                 System.out.println();
                                 System.out.println("Output Array"); 
                                 for (col=0;col< rrnums[0].length; col++){ 
                                 for (int i=0;i<rrnums.length;i++)
                                 for (int j=i;j<rrnums.length;j++){ 
                                 if (rrnums[i][col]>rrnums[j][col]){ 
                                 int temp=rrnums[i][col];
                                 rrnums[i][col]=rrnums[j][col]; 
                                 rrnums[j][col]=temp; } 
                                 } 
                          } 
                                 // prints the contents of the array
                                 int maxRow = 0;
                                 for (row = 0; row < rrnums.length; row++){ 
                                 for (col = 0; col < rrnums[row].length; col++){ 
                                 maxRow += rrnums[0][col];
                                 System.out.print( rrnums[row][col] + "  "); } 
                                 System.out.print( " = " + maxRow );
                                 System.out.println(); 
                                 // moves to next line 
                                 } 
                                 } 
                          }
                          • 10. Re: Java 2D sorting array with sums in output
                            807580
                            You initialize maxRow to zero only once, when you declare it. You need to set it back to zero each time you start a new row.

                            Also it's not clear to me what you're trying to do in the second to last set of nested loops. (with the temp variable) Are you trying to sort the rows there?

                            You should follow a standard indentation scheme. It makes it easier for others to follow your code. I believe there's a PDF entitled "Java Coding Conventions" somewhere on sun.com -- maybe Google can find it with the site: and filetype: search verbs.
                            • 11. Re: Java 2D sorting array with sums in output
                              699554
                              datnokka wrote:
                              Thanks, but like I said, I am lost man. Can you point me in the right direction. With Java I seem to learn best by reverse engineering. Is there a program similar to mine that I can look at that will help me see it on screen so I can better understand how to solve my problem? I don't want anyone to do my work for me, I just have to see it.
                              Heres my advice. When you find yourself lost, grab a pen and paper and try to solve the problem without any code involved. Essentially what you will be doing is abstracting the main components to solve a problem, hiding any miscellaneous detail. To verify what you have written or drawn is correct, try run through an example that could pass or fail.

                              Finally when you feel comfortable with how to solve the problem, implement each component in small steps. For example a method could be produced to display the contents of a 2-dimensional array. Make sure to separate this segment of code from the rest of your program. Another example could be generating a 2-dimensional array given two parameters, the width and height, returning the 2-dimensional array. Again this would be separated from the rest of the program, extracted into its own method.

                              Finally if and when you get stuck on a problem, you will find because you have split a large problem into smaller problems, that the problem isn't so hard to solve after all.

                              Mel
                              • 12. Re: Java 2D sorting array with sums in output
                                807580
                                Yeah I am sorting them low to high, top to bottom and then trying to add them. I am sooooo close. I have gotten in to print this.

                                Input Array:
                                8 6 8 9 5
                                3 6 2 5 8
                                8 3 5 4 8
                                3 7 7 0 3
                                6 3 0 7 1

                                Output Array
                                3 3 0 0 1 = 7
                                3 3 2 4 3 = 22
                                6 6 5 5 5 = 49
                                8 6 7 7 8 = 85
                                8 7 8 9 8 = 125

                                But as you can see, it adds the first row correctly, but instead of starting over on the second it adds the second to the first and so on. I knwo it is something so simple, but my brain is fried.
                                Here is the latest code.
                                import java.util.Random;
                                public class SortArray { 
                                
                                     public static void main(String[] args) { 
                                     System.out.print("Input Array:"); 
                                     System.out.println(); 
                                     int row, col; int[][] rrnums; 
                                     // array declaration
                                     rrnums = new int[5][5]; 
                                     // creation of 5 x 5 array
                                      Random random = new Random(); 
                                     // fills a 5 x 5 array with rrnums numbers between 0 and 9
                                      for (row = 0; row < rrnums.length; row++){ 
                                      for (col = 0; col < rrnums[row].length; col++){ 
                                      rrnums[row][col] = random.nextInt(10);
                                      //0-9 , 9 included
                                       } 
                                       }
                                      // prints the contents of the array
                                       for (row = 0; row < rrnums.length; row++){
                                       for (col = 0; col < rrnums[row].length; col++){ 
                                       System.out.print( rrnums[row][col] + " "); } 
                                       System.out.println(); 
                                       // moves to next line
                                        } 
                                       System.out.println();
                                       System.out.println("Output Array"); 
                                       for (col=0;col< rrnums[0].length; col++){ 
                                       for (int i=0;i<rrnums.length;i++)
                                       for (int j=i;j<rrnums.length;j++){ 
                                       if (rrnums[i][col]>rrnums[j][col]){ 
                                       int temp=rrnums[i][col];
                                       rrnums[i][col]=rrnums[j][col]; 
                                       rrnums[j][col]=temp; } 
                                       } 
                                } 
                                       // prints the contents of the array
                                       int maxRow = 0;
                                       for (row = 0; row < rrnums.length; row++){ 
                                       for (col = 0; col < rrnums[row].length; col++){ 
                                       maxRow += rrnums[row][col];
                                       System.out.print( rrnums[row][col] + "  "); } 
                                       System.out.print( " = " + maxRow);
                                       System.out.println(); 
                                       // moves to next line 
                                       } 
                                       } 
                                }
                                • 13. Re: Java 2D sorting array with sums in output
                                  807580
                                  I told you how to fix it in reply #10.
                                  • 14. Re: Java 2D sorting array with sums in output
                                    807580
                                    And by the way I don't see how you're sorting the rows. But you should probably put that off for later.
                                    1 2 Previous Next