Results 1 to 10 of 10
  1. #1
    jerb is offline Member
    Join Date
    May 2013
    Posts
    5
    Rep Power
    0

    Default how to add 2d arrays rows and columns?

    how to add 2d arrays rows and columns?

    having this given numbers:

    15 20 25 30
    18 21 24 27
    21 24 27 30

    the output will be:

    15 20 25 30 = 90
    18 21 24 27 = 90
    21 24 27 30 = 102
    -------------------
    54 65 76 87

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

    Default Re: how to add 2d arrays rows and columns?

    Think of row 'r'; adding all elements of row 'r' results in:

    Java Code:
    sum= array[r][0]+array[r][1]+array[r][2]+array[r][3]+ ...
    The same holds for column 'c':

    Java Code:
    sum= array[0][c]+array[1][c]+array[2][c]+array[3][c]+ ...
    Both sequences beg for a simple loop ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    jerb is offline Member
    Join Date
    May 2013
    Posts
    5
    Rep Power
    0

    Default Re: how to add 2d arrays rows and columns?

    can i ask for the complete java program for particuller given? pls. thanks

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

    Default Re: how to add 2d arrays rows and columns?

    For requests like that, a better forum is www.rentacoder.com

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    jerb is offline Member
    Join Date
    May 2013
    Posts
    5
    Rep Power
    0

    Default add arrays

    what to insert in this program to add the rows an columns?


    public class TwoDimArray1
    {
    public static void main(String[] args)
    {
    double[][] a = {
    { 1.0, 2.0, 3.0, 4.0 },
    { 2.0, 5.0, 1.0, 7.0 },
    { 4.0, 1.0, 2.0, 8.0 }
    };



    int i, j;




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


    for ( j = 0 ; j < a[i].length ; j++ )
    {
    System.out.print( a[i][j] + " " );
    }

    System.out.println();
    }

    }
    }

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: how to add 2d arrays rows and columns?

    previous post merged into this thread

    @OP: Please don't double post the same question.

  7. #7
    jerb is offline Member
    Join Date
    May 2013
    Posts
    5
    Rep Power
    0

    Default what to insert to add the rows and columns of this?

    public class TwoDimArray1
    {
    public static void main(String[] args)
    {
    double[][] a = {
    { 1.0, 2.0, 3.0, 4.0 },
    { 2.0, 5.0, 1.0, 7.0 },
    { 4.0, 1.0, 2.0, 8.0 }
    };



    int i, j;




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


    for ( j = 0 ; j < a[i].length ; j++ )
    {
    System.out.print( a[i][j] + " " );
    }

    System.out.println();
    }

    }
    }

  8. #8
    jerb is offline Member
    Join Date
    May 2013
    Posts
    5
    Rep Power
    0

    Default Re: how to add 2d arrays rows and columns?

    pls help.

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: how to add 2d arrays rows and columns?

    It's important to break things into small steps. Begin by trying to print the row totals.

    In the for loop print the number but also add it to another variable, so that at the end of the row you can print the total.

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,304
    Blog Entries
    7
    Rep Power
    20

    Default Re: how to add 2d arrays rows and columns?

    @OP: again, don't flood this forum with threads containing the same question. Pbrockway2 already merged one thread, I merged the other; don't do that anymore.

    kind regards,

    Jos.

    ps. tip: first add every row in a loop; next add the columns in another loop; optimization is for later ...
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Adding the sum of the columns and rows in a 2d array
    By spsu_girl in forum New To Java
    Replies: 1
    Last Post: 02-28-2013, 03:46 AM
  2. Replies: 0
    Last Post: 02-27-2012, 09:13 PM
  3. Replies: 8
    Last Post: 05-16-2011, 06:35 AM
  4. Need a loop for rows and columns
    By Ceasar in forum New To Java
    Replies: 4
    Last Post: 10-09-2009, 03:03 PM
  5. 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

Posting Permissions

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