Results 1 to 9 of 9
- 10-07-2012, 04:20 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 3
- Rep Power
- 0
[HELP] How to get the sum of column in 2d array
Java Code:import java.util.Scanner; public class SumRowCols { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int numrows=0; int numcols=0; long sum=0; System.out.print("How many row? "); numrows=sc.nextInt(); System.out.print("How many columns? "); numcols=sc.nextInt(); long [][]aArray = new long[numrows][numcols]; for (int r = 0; r < numrows; ++r){ for(int c = 0; c < numcols; ++c){ System.out.print("Enter number at row"+" "+ (r+1) + " "+"column"+" " + (c+1)+": " ); aArray[r][c]=sc.nextLong(); } } System.out.println("\nArray and Sum"); for(int r = 0; r < numrows; r++){ for (int c = 0; c < numcols; c++){ System.out.print(aArray[r][c]+"\t"); sum+=aArray[r][c]; } System.out.print(sum); sum=0; System.out.println(); } } }
this is the outpu format :
the question is how to get the sum of column???Java Code:How many row? 2 How many columns? 2 Enter number at row 1 column 1: 2 Enter number at row 1 column 2: 2 Enter number at row 2 column 1: 2 Enter number at row 2 column 2: 2 Array and Sum 2 2 4 2 2 4 ? ?
thanks for those who help????Last edited by ledster; 10-07-2012 at 04:35 PM.
-
Re: [HELP] How to get the sum of column in 2d array
How is your current program misbehaving?
Please edit your original post and change your [quote] [/quote] tags to [code] [/code] so your posted code will retain its formatting and be readable.
- 10-07-2012, 10:42 PM #3
Member
- Join Date
- Oct 2012
- Location
- Tempe, Arizona
- Posts
- 77
- Blog Entries
- 12
- Rep Power
- 0
Re: [HELP] How to get the sum of column in 2d array
If there is just two columns, and they are staying constant, then you could just do:
Java Code:for(int r = 0; r < numrows; r++){ for (int c = 0; c < numcols; c++){ System.out.print(aArray[r][c]+"\t"); sum+=aArray[r][c]; sum1+=aArray[r][1]; sum2+=aArray[r][2]; }
- 10-08-2012, 05:05 PM #4
Member
- Join Date
- Oct 2012
- Posts
- 3
- Rep Power
- 0
Re: [HELP] How to get the sum of column in 2d array
i do what you've said but it gives me an error.....
- 10-08-2012, 05:46 PM #5
Member
- Join Date
- Oct 2012
- Location
- Tempe, Arizona
- Posts
- 77
- Blog Entries
- 12
- Rep Power
- 0
Re: [HELP] How to get the sum of column in 2d array
What is the error?
Now that I look at the code again, it should be:
Java Code:for(int r = 0; r < numrows; r++){ for (int c = 0; c < numcols; c++){ System.out.print(aArray[r][c]+"\t"); sum+=aArray[r][c]; } sum1+=aArray[r][1]; sum2+=aArray[r][2]; }
This is because is should only calculate these sums once for each row. How I had it set up, it was calculating it for each row, and each column; but if the columns are specified as constants, then there is no need for the added complexity. My bad. Though I don't think it should have given you an error, just some crazy numbers.. If you are still getting an error, then copy and paste so that we can see exactly what the compiler is saying.
- 10-08-2012, 06:11 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: [HELP] How to get the sum of column in 2d array
Please don't spoonfeed.
Please do not ask for code as refusal often offends.
- 10-08-2012, 08:26 PM #7
Member
- Join Date
- Oct 2012
- Posts
- 3
- Rep Power
- 0
Re: [HELP] How to get the sum of column in 2d array
can you please make the code complete because it is really an error i copy paste what you've post and then after i run its error???
-
Re: [HELP] How to get the sum of column in 2d array
- 10-09-2012, 02:26 AM #9
Member
- Join Date
- Oct 2012
- Location
- Tempe, Arizona
- Posts
- 77
- Blog Entries
- 12
- Rep Power
- 0
Similar Threads
-
Trying to sum a column of a 2d array....
By hoppedUpOnJava in forum New To JavaReplies: 1Last Post: 10-06-2011, 08:36 PM -
I want to know how to calculate and display the sum of each column in 2D array
By Javanoobs in forum New To JavaReplies: 1Last Post: 03-03-2011, 06:43 AM -
How to write column by column to text file
By trkece in forum JDBCReplies: 9Last Post: 02-15-2011, 01:13 AM -
selecting column names dynamically from table column header
By neha_sinha in forum AWT / SwingReplies: 1Last Post: 07-06-2010, 04:50 PM -
Extracting a Row/Column from An Array
By besweeet in forum New To JavaReplies: 10Last Post: 04-18-2010, 07:56 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks