Results 1 to 8 of 8
  1. #1
    John_shok is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Default how to add columns from Matrices(2D array)

    ex:

    1 2
    3 4
    ------
    4 6


    I was successful to add the rows but i couldnt figure out how to add the column of this array!

    Thank you

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by John_shok View Post
    ex:

    1 2
    3 4
    ------
    4 6


    I was successful to add the rows but i couldnt figure out how to add the column of this array!

    Thank you
    Can you show us how you added all the rows together? Then we'll show you how to do the column additions. So the result would be:

    Java Code:
    1 2 | 3
    3 4 | 7
    kind regards,

    Jos

  3. #3
    John_shok is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Default

    int[][] a=new int[2][2];
    int m=0;
    int sum=0;



    for(int j=0;j<a.length;j++){

    for(int k=0;k<a[j].length;k++){
    m=m+1;
    a[j][k]=m;
    System.out.print(a[j][k]+ " ");
    sum+=m;
    }

    System.out.print(" >>> "+ sum);
    sum=0;
    System.out.println();
    }

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by John_shok View Post
    int[][] a=new int[2][2];
    int m=0;
    int sum=0;



    for(int j=0;j<a.length;j++){

    for(int k=0;k<a[j].length;k++){
    m=m+1;
    a[j][k]=m;
    System.out.print(a[j][k]+ " ");
    sum+=m;
    }

    System.out.print(" >>> "+ sum);
    sum=0;
    System.out.println();
    }
    It's too bad you've done everything at the same time and in the same method. I prefer a couple of separate methods, one that adds all values in a row of a matrix and one that adds all values in a column of a matrix. Here they are:

    Java Code:
    int addRow(int[][] matrix, int row) {
       int sum= 0;
       for (int col= 0; col < matrix[row].length; col++)
          sum+= matrix[row][col];
       return sum;
    }
    
    int addCol(int[][] matrix, int col) {
       int sum= 0;
       for (int row= 0; row < matrix.length; row++)
          sum+=  matrix[row][col];
       return sum;
    }
    As you can see both methods look alike, but one iterates over a single row (the first method) while the other one iterates over a single column (the second method).

    kind regards,

    Jos

  5. #5
    John_shok is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Default

    Thank u josh! it is beautiful... I created a new class for addRow and addCol and ran it through driver class.. I was just wondering how one could get this kind of idea.. :D

  6. #6
    John_shok is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Default

    I thought it worked out good.. but when i tried for 2x3 matrix, it created a problem
    created a class for addrow
    create a class for colrow
    created a drive class

    >>AddRow ar=new AddRow();
    for(int i=0;i<num.length;i++){
    int sumRow = ar.addRow(num, i);
    System.out.println("Row"+(i+1)+": "+sumRow);

    BUt for colum??
    for(int i=0;i<num.length[row];i++){???

    i cannot do this?? if i do only num.length[]... this will print out only till 2 columns ,, it is supposed to be 3..

    for row it worked out fine..

  7. #7
    John_shok is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Default

    Josh Thank you again.. I have figure it out ..

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by John_shok View Post
    Josh Thank you again.. I have figure it out ..
    Good; I don't know what your problem was because my methods only assume that the matrix isn't 'ragged', i.e. just rectangular or square, and work fine for non-square matrixes.

    kind regards,

    Jos

Similar Threads

  1. Read two matrices from file
    By zenitis in forum New To Java
    Replies: 13
    Last Post: 05-20-2010, 03:03 AM
  2. Counting the number of columns in a 2D array,
    By KalEl in forum New To Java
    Replies: 9
    Last Post: 10-21-2008, 05:27 AM
  3. Two diM aRRay and add rows and columns....
    By filly444 in forum New To Java
    Replies: 2
    Last Post: 08-30-2008, 05:24 PM
  4. How to multiply two matrices
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:50 PM
  5. implementing sparse and nonsparse matrices together
    By ishakteyran in forum New To Java
    Replies: 0
    Last Post: 12-07-2007, 08:10 PM

Posting Permissions

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