Results 1 to 9 of 9
  1. #1
    ledster is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Default [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. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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. #3
    penguinCoder is offline Member
    Join Date
    Oct 2012
    Location
    Tempe, Arizona
    Posts
    77
    Blog Entries
    12
    Rep Power
    0

    Default 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. #4
    ledster is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Default 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. #5
    penguinCoder is offline Member
    Join Date
    Oct 2012
    Location
    Tempe, Arizona
    Posts
    77
    Blog Entries
    12
    Rep Power
    0

    Default 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. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

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

    ** This space for rent **

  7. #7
    ledster is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Default 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. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

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

    Quote Originally Posted by ledster View Post
    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. #9
    penguinCoder is offline Member
    Join Date
    Oct 2012
    Location
    Tempe, Arizona
    Posts
    77
    Blog Entries
    12
    Rep Power
    0

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

Similar Threads

  1. Replies: 11
    Last Post: 10-21-2014, 05:46 AM
  2. Trying to sum a column of a 2d array....
    By hoppedUpOnJava in forum New To Java
    Replies: 1
    Last Post: 10-06-2011, 09:36 PM
  3. Replies: 1
    Last Post: 03-03-2011, 07:43 AM
  4. Replies: 1
    Last Post: 07-06-2010, 05:50 PM
  5. Extracting a Row/Column from An Array
    By besweeet in forum New To Java
    Replies: 10
    Last Post: 04-18-2010, 08:56 AM

Posting Permissions

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