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

1. Member Join Date
Feb 2011
Posts
18
Rep Power
0

## 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  Reply With Quote

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

Java 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  Reply With Quote

3. Member Join Date
Feb 2011
Posts
18
Rep Power
0

## 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?  Reply With Quote

4. Member Join Date
Feb 2011
Posts
18
Rep Power
0

## and also do i need to cr8 a new class for calculating the array? or do it in the main?  Reply With Quote

5. ##  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  Reply With Quote

6. ## 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.  Reply With Quote

7. Member Join Date
Feb 2011
Posts
18
Rep Power
0

##  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...  Reply With Quote

8. Member Join Date
Feb 2011
Posts
18
Rep Power
0

##  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...:)  Reply With Quote

9. ## How would you find the sum of a single array? Each index in a 2d array is actually a single array

Java Code:
```int[][] x = new int[];
int = { 1, 2, 3 };
int = { 4, 5, 2 };
int = { 6, 8, 9 };
int = { 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.  Reply With Quote

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

Java 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:

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

kind regards,

Jos  Reply With Quote

11. ## generic, anonymously class, interface )
Java 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) {
}
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;
}
}```  Reply With Quote

12. Member Join Date
Feb 2011
Posts
18
Rep Power
0

## thanks to josah and sunde...i know how it goes along now...thanks you guys help, and sorry for disturbing you both...peace =)  Reply With Quote

13. Member Join Date
Feb 2011
Posts
18
Rep Power
0

##  Originally Posted by Petr generic, anonymously class, interface )
Java 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) {
}
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...  Reply With Quote

#### Posting Permissions

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