# How to get sum of column in array?

Page 1 of 1

## 13 Replies - 1063 Views - Last Post: 30 November 2012 - 11:17 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=301932&amp;s=c6705e33315419315be0fa775fe107bb&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Odko

Reputation: -1
• Posts: 37
• Joined: 25-October 12

# How to get sum of column in array?

Posted 28 November 2012 - 12:00 PM

Hello guys!
I am looking for some help on my programming.I need to find sum of each column in 2d array. The result suppose to be like this.: {817.192, 471,1821, 144.7155, 115.6173}
Here is my code please take look.
import java.io.*;

public class Columnsun {
public static void main(String[] args) {
double[][] initialArray = { { 7.432, 8.541, 23.398, 3.981 },
{ 721.859, 6.9211, 29.7505, 53.6483 },
{ 87.901, 455.72, 91.567, 57.988 } };
}

public void columnSum(double[][] initialArray) {
double sum = 0;
for (int column = 0; column < initialArray.length-1; column++) {
sum = 0;
for (int row = 0; row < initialArray.length; row++)
sum = initialArray[row].length;

// the sum of each row.
// for(int row = 0; row <= initialArray.length ; row++){
// sum = 0;
// for(int col = 0; col<initialArray[(int) row].length; col++){

// sum += initialArray[ row][ col];

}
// System.out.println("The sum of row" + row +"is"+sum);
System.out.print(sum + "\t");
return;
}

}

I don't have any compile error, but there is nothing print out.

I don't have any compile error, but there is nothing print out.

Is This A Good Question/Topic? 0

## Replies To: How to get sum of column in array?

### #2 jon.kiparsky

• Pancakes!

Reputation: 8770
• Posts: 15,098
• Joined: 19-March 11

## Re: How to get sum of column in array?

Posted 28 November 2012 - 12:03 PM

Is this the code you're actually running? You don't call the columnSum method here.

It won't work, but at least that'll be a start.

### #3 blackcompe

• D.I.C Lover

Reputation: 1156
• Posts: 2,538
• Joined: 05-May 05

## Re: How to get sum of column in array?

Posted 28 November 2012 - 12:08 PM

A method named columnSum should have an argument that specifies which column to sum. E.g.

int sumColumn(int[][] ar, int col);

It's pretty much as simple as the following:

int sum = 0;
for(int[] row : ar)
sum += row[col];
return sum;

### #4 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: How to get sum of column in array?

Posted 28 November 2012 - 12:18 PM

or make it double[] for the sum of each colum

double[] columSum(double[][] array) {
double[] sum = new double[array[0].length];
for(int i = 0; i < array.length; ++i) {
for(int j = 0; j < array[i].length; j++) {
sum[j] += array[i][j];
}
}

return sum;
}

### #5 Odko

Reputation: -1
• Posts: 37
• Joined: 25-October 12

## Re: How to get sum of column in array?

Posted 28 November 2012 - 12:34 PM

Thanks Blackcompe. but the thing is my result supposes to be double which means i can cot use int right?

pbl
Is i column and j is row ?

### #6 ipushmycar

• D.I.C Regular

Reputation: 86
• Posts: 390
• Joined: 29-August 10

## Re: How to get sum of column in array?

Posted 28 November 2012 - 12:45 PM

Odko, on 28 November 2012 - 02:34 PM, said:

Thanks Blackcompe. but the thing is my result supposes to be double which means i can cot use int right?

pbl
Is i column and j is row ?

EDIT: 'j' is the column and 'i' is the row.

This post has been edited by ipushmycar: 28 November 2012 - 12:49 PM

### #7 Odko

Reputation: -1
• Posts: 37
• Joined: 25-October 12

## Re: How to get sum of column in array?

Posted 28 November 2012 - 12:48 PM

Sorry but still it doesn't work
public double[] columnSum(double[][] initialArray) {

double[] sum = new double[initialArray[0].length];
for (int row = 0; row < initialArray.length; row++) {
for (int column = 0; column < initialArray.length; column++) {
sum[column] += initialArray[row][column];
}
// the sum of each row.
// for(int row = 0; row <= initialArray.length ; row++){
// sum = 0;
// for(int col = 0; col<initialArray[(int) row].length; col++){

// sum += initialArray[ row][ col];

}
System.out.print(sum + "\t");
// System.out.println("The sum of row" + row +"is"+sum);
return sum;
}

### #8 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: How to get sum of column in array?

Posted 28 November 2012 - 01:00 PM

for the number of column should be

initialArray[row].length;

or your column will go from 0 to the number of rows in the array

as in my original code
for(int j = 0; j < array[i].length; j++) {

### #9 jon.kiparsky

• Pancakes!

Reputation: 8770
• Posts: 15,098
• Joined: 19-March 11

## Re: How to get sum of column in array?

Posted 28 November 2012 - 01:31 PM

What isn't working? Please get in the habit of stating what your problem is. "It doesn't work" is not useful.

### #10 Odko

Reputation: -1
• Posts: 37
• Joined: 25-October 12

## Re: How to get sum of column in array?

Posted 28 November 2012 - 03:24 PM

If i know the problem why it does not work i will not ask. Seems like i wrote code but does not happen. when i run my program nothing print out.

### #11 GregBrannon

• D.I.C Lover

Reputation: 2216
• Posts: 5,241
• Joined: 10-September 10

## Re: How to get sum of column in array?

Posted 28 November 2012 - 03:36 PM

When you go to a doctor, and the doctor says, "What's wrong?" Do you say, "I have lung cancer?" No. You say, "It hurts when I breathe."

Jon's not asking you to provide a diagnosis but to give us the symptoms so that we know what "It doesn't work" means. "Nothing prints out," is a start, but I'll bet you can do better.

What is supposed to print out?
Why should the program print anything?
Do you have any ideas/guesses or have you done any troubleshooting to determine why nothing prints? If so, what did you learn? If not, why not?

These are some of the basic questions you should have already asked yourself before making some effort to answer them.

This post has been edited by GregBrannon: 28 November 2012 - 03:37 PM

### #12 jon.kiparsky

• Pancakes!

Reputation: 8770
• Posts: 15,098
• Joined: 19-March 11

## Re: How to get sum of column in array?

Posted 28 November 2012 - 04:52 PM

Thank you, Greg. Yes, that's exactly what I mean.

### #13 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: How to get sum of column in array?

Posted 28 November 2012 - 07:47 PM

Your code is wrong as explained in my post #8

You didn't correctly cut & paste and replace with your variable the posted code
you only compile the column from to to rowCount

### #14 Odko

Reputation: -1
• Posts: 37
• Joined: 25-October 12

## Re: How to get sum of column in array?

Posted 30 November 2012 - 11:17 AM

Thank you for all the people's help. Finally, I finished my program. It works perfectly as I wanted.
Best regards