Results 1 to 5 of 5
  1. #1
    osborne is offline Member
    Join Date
    Dec 2008
    Posts
    2
    Rep Power
    0

    Default Please Help me w/ this Problem...

    This is the Output i want to work out using multidimensional array?
    This is the output...
    the sum to the 1st row and sum of the 2nd column...

    1 2 3 = 6
    4 5 6 = 15
    7 8 9 = 24
    121518

    Please help me with this one... im new in java...

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    An array of sums and two nested loops.

    Give it a try, and then post the problematic code and give a complete description of your actual problem.

  3. #3
    osborne is offline Member
    Join Date
    Dec 2008
    Posts
    2
    Rep Power
    0

    Default this are the code im working....

    public class Main {
    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    int[][] matrix = new int[4][3];
    int row, col, value = 1;
    final int NUM_ROWS = matrix.length;
    final int NUM_COLS = matrix[0].length;

    for (row = 0; row < NUM_ROWS; row++) {
    for (col = 0; col < NUM_COLS; col++) {
    matrix[row][col] = value;
    value++;
    }
    }

    for (row = 0; row < NUM_ROWS; row++) {
    for (col = 0; col < NUM_COLS; col++) {
    System.out.print(matrix[row][col] + "\t");
    }
    System.out.println();
    }

    }


    }



    I need help with my code that will find the sum of the 3 rows which equal 6, 15, 24, 33 then add the columns that equal 22, 26 , 30.
    here is my code so far:

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Well, like I said, create a single dimensional array for the sums (that will correlate, of course, to the second dimension in your 2 dimensional array). Then, using nested loops, sum the values from the second dimension columns using the same index into the sum array.

    At least try it.

  5. #5
    Sven is offline Member
    Join Date
    Dec 2008
    Location
    Enschede, Overijssel, the Netherlands
    Posts
    19
    Rep Power
    0

    Default

    Java Code:
    public class Main {
    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
        int[][] matrix = new int[4][3];
        int row, col, value = 1;
        final int NUM_ROWS = matrix.length;
        final int NUM_COLS = matrix[0].length;
    
        for (row = 0; row < NUM_ROWS; row++) {
            for (col = 0; col < NUM_COLS; col++) {
                matrix[row][col] = value;
                value++;
            }
        }
    
        for (row = 0; row < NUM_ROWS; row++) {
                 for (col = 0; col < NUM_COLS; col++) {
                      System.out.print(matrix[row][col] + "\t");
                 }
                 System.out.println();
             }
        }
    }
    Please use code tags for formatting.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •