# Thread: [HELP] How to get the sum of column in 2d array

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 :

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
?   ?```
the question is how to get the sum of column???
thanks for those who help????
Last edited by ledster; 10-07-2012 at 05:35 PM.

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

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];
}```

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.....

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.

6. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
27

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???

8. ## Re: [HELP] How to get the sum of column in 2d array

Originally Posted by ledster
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???
Please don't request full solutions. Show what you've done, show any errors, and let us help you learn Java, not learn how to beg for code.

9. 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

Yea, I can't just post the whole code for you, it would get deleted before you could read it anyways. Just copy and paste your new code in here, along with the error that you are getting.

#### Posting Permissions

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