# How to write a program to calculate and display sum in two dimensional array?

• 02-08-2011, 08:33 AM
Javanoobs
How to write a program to calculate and display sum in two dimensional array?
I have a college tutorial question that required me to write a program which need to calculate and display the sum of each column of a two dimensional array. For example, in the following array

2 6 -1 5
8 2 7 -5
1 0 4 4

the method shoudl display the following
The sum of column 0 is 11
The sum of column 1 is 8
The sum of column 2 is 10
The sum of column 3 is 4

Assume that in the calling program the array A is declared as float A[MAXROW][MAXCOL]; where MAXROW and MAXCOL are global int constants.

The method should have a two dimensional array as its only parameter and shold not return any values.

I have roughly wrote something on a paper, but i jst dun get the idea how to link all the tings 2gether, can anyone pls help me of this?

public class array
public static final MAXROW{
public static final MAXCOL{

public void calculate Sum(){
float A[MAXROW][MAXCOL];
for (int i = 0; i<MAXROW.length; i++)
{
for (int j = 0; j<MAXCOL.length;j++)
{
(I don't have idea in tis part)
}
}

i knw my code is poor and not completed, but im in the progress of learning, im using netbean 6.9.1, pls help me out ..thx
• 02-08-2011, 10:04 AM
JosAH
Do it 'bottom up'; i.e. calculate the sum of one column; let j be the column for which you have to add all the numbers of a two dimensional array 'a'. The number of rows is known, i.e. a.length, so finding that total is just one loop:

Code:

```int sum= 0; for (int row= 0; row < a.length; a++)   sum+= a[row][j];```
All you have to do is apply the above code snippet to all columns of the array and add a few System.out.println( ... ) statements.

kind regards,

Jos
• 02-08-2011, 11:17 AM
Javanoobs
may i knw y it ask me to declared the [maxrow][maxcol] and declared it as global int? if lik u say apply 1 loop, and how to deal wit tis maxrow and maxcol?
• 02-08-2011, 11:32 AM
Javanoobs
and also do i need to cr8 a new class for calculating the array? or do it in the main?
• 02-08-2011, 12:49 PM
JosAH
Quote:

Originally Posted by Javanoobs
may i knw y it ask me to declared the [maxrow][maxcol] and declared it as global int? if lik u say apply 1 loop, and how to deal wit tis maxrow and maxcol?
and also do i need to cr8 a new class for calculating the array? or do it in the main?

If you want to ask me a question please use (more or less) proper English; I also reply in (more or less) proper English.

kind regards,

Jos
• 02-08-2011, 02:56 PM
sunde887
Im having a hard time understanding your questions, but I think the easiest way to do this would be to create a static method to produce the sum of a single array, then loop through the 2d array applying the static method and storing the values in a new array.
• 02-08-2011, 03:15 PM
Javanoobs
Quote:

Originally Posted by JosAH
If you want to ask me a question please use (more or less) proper English; I also reply in (more or less) proper English.

kind regards,

Jos

my apologize...because I am having hard time to understand your explanation, Im stil a beginner in this, so hope you could explain this in details, you help will be fully appreciated...
• 02-08-2011, 03:17 PM
Javanoobs
Quote:

Originally Posted by sunde887
Im having a hard time understanding your questions, but I think the easiest way to do this would be to create a static method to produce the sum of a single array, then loop through the 2d array applying the static method and storing the values in a new array.

ok...can you provide some coding example so I can get exactly what you said? your help will be kindly appreciated...:)
• 02-08-2011, 03:21 PM
sunde887
How would you find the sum of a single array? Each index in a 2d array is actually a single array

Code:

```int[][] x = new int[4][]; int[0] = { 1, 2, 3 }; int[1] = { 4, 5, 2 }; int[2] = { 6, 8, 9 }; int[3] = { 2, 5, 9 , 10 };```
is a 2d array, so it simply comes down to finding the sum of a single array then applying that process to each element of a 2d array.
• 02-08-2011, 03:26 PM
JosAH
Quote:

Originally Posted by Javanoobs
my apologize...because I am having hard time to understand your explanation, Im stil a beginner in this, so hope you could explain this in details, you help will be fully appreciated...

As I wrote you should apply my code snippet to a single column j of your matrix. It would be easiest to turn it into a little method, like this:

Code:

```private void calculateColumn(int[][] array, int j) {   // my code snippet goes here   // and a System.out.println( ... ) statement goed here }```
If you want to print the results of all columns you should apply the method above to each and every column of your matrix, like this:

Code:

```public void calculate(int[][] array) {   for (int col= 0; col < array[0].length; col++)       calculateColumn(array, col); }```
That's all the code it takes ...

kind regards,

Jos
• 02-08-2011, 03:52 PM
Petr
generic, anonymously class, interface )
Code:

```import java.util.ArrayList; import java.util.List; public class SumEx {     public static void main(String[] arg) {         Integer[][] mass = {                 {2, 6, -1, 5},                 {8, 2, 7, -5},                 {1, 0, 4, 4},         };         System.out.println("Summ column 0 : " + summ(build(mass, 0)));         System.out.println("Summ column 1 : " + summ(build(mass, 1)));         System.out.println("Summ column 2 : " + summ(build(mass, 2)));         System.out.println("Summ column 3 : " + summ(build(mass, 3)));     }     private static <T extends Number> List<T> build(T[][] mass, int index) {         List<T> list = new ArrayList<T>();         for (T[] t : mass) {             list.add(t[index]);         }         return list;     }     private static <T extends Number> double summ(List<T> list) {         return forEach(list, new Operation<Number>() {             @Override             public Number invoke(Number result, Number number) {                 return result.doubleValue() + number.doubleValue();             }         });     }     interface Operation<A extends Number> {         public A invoke(A result, A a);     }     private static <T extends Number> double forEach(List<T> list, Operation<Number> operation) {         double result = 0D;         for (T t : list) {             result = operation.invoke(result, t).doubleValue();         }         return result;     } }```
• 02-08-2011, 03:54 PM
Javanoobs
thanks to josah and sunde...i know how it goes along now...thanks you guys help, and sorry for disturbing you both...peace =)
• 02-08-2011, 03:58 PM
Javanoobs
Quote:

Originally Posted by Petr
generic, anonymously class, interface )
Code:

```import java.util.ArrayList; import java.util.List; public class SumEx {     public static void main(String[] arg) {         Integer[][] mass = {                 {2, 6, -1, 5},                 {8, 2, 7, -5},                 {1, 0, 4, 4},         };         System.out.println("Summ column 0 : " + summ(build(mass, 0)));         System.out.println("Summ column 1 : " + summ(build(mass, 1)));         System.out.println("Summ column 2 : " + summ(build(mass, 2)));         System.out.println("Summ column 3 : " + summ(build(mass, 3)));     }     private static <T extends Number> List<T> build(T[][] mass, int index) {         List<T> list = new ArrayList<T>();         for (T[] t : mass) {             list.add(t[index]);         }         return list;     }     private static <T extends Number> double summ(List<T> list) {         return forEach(list, new Operation<Number>() {             @Override             public Number invoke(Number result, Number number) {                 return result.doubleValue() + number.doubleValue();             }         });     }     interface Operation<A extends Number> {         public A invoke(A result, A a);     }     private static <T extends Number> double forEach(List<T> list, Operation<Number> operation) {         double result = 0D;         for (T t : list) {             result = operation.invoke(result, t).doubleValue();         }         return result;     } }```

once again, thanks to you peter for your help, but do i need to do so much abstract coding? I mean this is just a simple array calculation program, just my opinion...hope to seek your advice...