Results 1 to 2 of 2
- 03-03-2011, 06:02 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 18
- Rep Power
- 0
I want to know how to calculate and display the sum of each column in 2D array
I want tp create method that calculate and display the sum of each column of a 2D array, for example :
2 6 -1 5
8 2 7 -5
1 0 4 4
The method should display as follow :
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
I have declared the array in the main program:
and have some method :Java Code:public class Main { static final int MAXROW; <----- MAXROW is a global int constant static final int MAXCOL; <----- MAXCOL is a global int constant public static void main(String[] args) { float[][]A = new float[MAXROW][MAXCOL]; }
im stuck in main part because I not very sure whether the method i created is working fine, somomore i not clear how to call the method in the main partJava Code:private void calculateColumn(int[][] array, int j) { int sum= 0; for (int row= 0; row < a.length; a++) sum+= a[row][j]; System.out.println("The sum of column" +j+ "is" + sum); } public void calculate(int[][] array) { for (int col= 0; col < array[0].length; col++) calculateColumn(array, col); }
I know the code seesm defect but this is all i got so far,sorry for the limitation of my knowledge...
any suggestion and correction are welcome
- 03-03-2011, 06:43 AM #2
Senior Member
- Join Date
- Jan 2011
- Location
- Bangalore, India
- Posts
- 102
- Rep Power
- 0
If you don't know how to call a method I'd suggest you learn some basics before you start.
You methods too, are almost wrong.
Instead of "j" and "a" it should be "row". Think about the logic.Java Code:for (int row= 0; row < a.length; a++) sum+= a[row][j];
This will calculate the sum of zero-th column. Now you think about a way how to calculate the sum of 1st and 2nd columns.
You need to learn about arrays also before proceeding
Similar Threads
-
How to write a program to calculate and display sum in two dimensional array?
By Javanoobs in forum New To JavaReplies: 12Last Post: 02-08-2011, 02:58 PM -
How to write a program to calculate and display sum in two dimensional array
By Javanoobs in forum Advanced JavaReplies: 1Last Post: 02-08-2011, 09:11 AM -
Datepicked column in Display Tag
By praveenkumar379 in forum Web FrameworksReplies: 0Last Post: 05-18-2010, 09:06 AM -
Java application to calculate and display the cost of a number of tickets according
By herberwz in forum New To JavaReplies: 1Last Post: 04-27-2010, 11:29 PM -
display column value multiply with 100
By tiiim83 in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 01-15-2009, 03:40 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks