[HELP] How to get the sum of column in 2d array
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 :
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????
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.
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:
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];
}
Re: [HELP] How to get the sum of column in 2d array
i do what you've said but it gives me an error.....
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:
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.
Re: [HELP] How to get the sum of column in 2d array
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
Quote:
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.
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.