Results 1 to 13 of 13
 02082011, 07:33 AM #1Member
 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
 02082011, 09:04 AM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,508
 Blog Entries
 7
 Rep Power
 20
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];
kind regards,
Joscenosillicaphobia: the fear for an empty beer glass
 02082011, 10:17 AM #3Member
 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?
 02082011, 10:32 AM #4Member
 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?
 02082011, 11:49 AM #5
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,508
 Blog Entries
 7
 Rep Power
 20
 02082011, 01:56 PM #6
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 8
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.
 02082011, 02:15 PM #7Member
 Join Date
 Feb 2011
 Posts
 18
 Rep Power
 0
 02082011, 02:17 PM #8Member
 Join Date
 Feb 2011
 Posts
 18
 Rep Power
 0
 02082011, 02:21 PM #9
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 8
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[4][]; int[0] = { 1, 2, 3 }; int[1] = { 4, 5, 2 }; int[2] = { 6, 8, 9 }; int[3] = { 2, 5, 9 , 10 };
 02082011, 02:26 PM #10
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,508
 Blog Entries
 7
 Rep Power
 20
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 }
Java Code:public void calculate(int[][] array) { for (int col= 0; col < array[0].length; col++) calculateColumn(array, col); }
kind regards,
Joscenosillicaphobia: the fear for an empty beer glass
 02082011, 02:52 PM #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) { 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; } }
Skype: petrarsentev
http://TrackStudio.com
 02082011, 02:54 PM #12Member
 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 =)
 02082011, 02:58 PM #13Member
 Join Date
 Feb 2011
 Posts
 18
 Rep Power
 0
Similar Threads

Program in Java To calculate GCD of n numbers.?
By ankitsinghal_89 in forum New To JavaReplies: 4Last Post: 02152011, 09:23 AM 
How to write a program to calculate and display sum in two dimensional array
By Javanoobs in forum Advanced JavaReplies: 1Last Post: 02082011, 09:11 AM 
Java application to calculate and display the cost of a number of tickets according
By herberwz in forum New To JavaReplies: 1Last Post: 04272010, 11:29 PM 
Multidimensional array programhelp!
By bobmasta5 in forum New To JavaReplies: 13Last Post: 03152009, 07:21 PM 
Why my program cannot calculate the decimal value?
By pearllymary78 in forum New To JavaReplies: 4Last Post: 06232008, 12:52 AM
Bookmarks