13 Replies - 649 Views - Last Post: 30 November 2012 - 11:17 AM Rate Topic: -----

#1 Odko  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7871
  • View blog
  • Posts: 13,349
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,535
  • 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;


Was This Post Helpful? 2
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • 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;
}


Was This Post Helpful? 0
  • +
  • -

#5 Odko  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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 ?
Was This Post Helpful? 0
  • +
  • -

#6 ipushmycar  Icon User is online

  • D.I.C Regular

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

Re: How to get sum of column in array?

Posted 28 November 2012 - 12:45 PM

View PostOdko, 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

Was This Post Helpful? 0
  • +
  • -

#7 Odko  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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;
	}


Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • 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++) {
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7871
  • View blog
  • Posts: 13,349
  • 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.
Was This Post Helpful? 1
  • +
  • -

#10 Odko  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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.
Was This Post Helpful? -1
  • +
  • -

#11 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • 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

Was This Post Helpful? 2
  • +
  • -

#12 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7871
  • View blog
  • Posts: 13,349
  • 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.
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • 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
Was This Post Helpful? 0
  • +
  • -

#14 Odko  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1