# Thread: how to add 2d arrays rows and columns?

1. Member
Join Date
May 2013
Posts
5
Rep Power
0

## 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. ## 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

3. Member
Join Date
May 2013
Posts
5
Rep Power
0

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

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

4. ## Re: how to add 2d arrays rows and columns?

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

kind regards,

Jos

5. Member
Join Date
May 2013
Posts
5
Rep Power
0

## 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. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
14

## 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. Member
Join Date
May 2013
Posts
5
Rep Power
0

## 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. Member
Join Date
May 2013
Posts
5
Rep Power
0

pls help.

9. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
14

## 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. ## 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 ...

#### Posting Permissions

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