# how to add 2d arrays rows and columns?

• 05-19-2013, 05:00 PM
jerb
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
• 05-19-2013, 05:51 PM
JosAH
Re: how to add 2d arrays rows and columns?
Think of row 'r'; adding all elements of row 'r' results in:

Code:

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

Code:

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

kind regards,

Jos
• 05-19-2013, 07:52 PM
jerb
Re: how to add 2d arrays rows and columns?
can i ask for the complete java program for particuller given? pls. thanks
• 05-19-2013, 08:10 PM
JosAH
Re: how to add 2d arrays rows and columns?
For requests like that, a better forum is www.rentacoder.com

kind regards,

Jos
• 05-20-2013, 01:31 AM
jerb
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();
}

}
}
• 05-20-2013, 01:36 AM
pbrockway2
Re: how to add 2d arrays rows and columns?
previous post merged into this thread

@OP: Please don't double post the same question.
• 05-20-2013, 01:41 AM
jerb
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();
}

}
}
• 05-20-2013, 01:46 AM
jerb
Re: how to add 2d arrays rows and columns?
pls help.
• 05-20-2013, 06:18 AM
pbrockway2
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.
• 05-20-2013, 09:19 AM
JosAH
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 ...