Results 1 to 4 of 4
  1. #1
    XxVashxX is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default 2d Array Sums Help

    Well, looks like this my first post. Need a little help with my homework. Anyway, here's what I have so far. Both #'s 2 and 3 are somewhat similar, but I'm stumped. The main method is given by default. Please && Thank you.

    Java Code:
        /* Step 2:  Write a method with the header
         
           public static int sumRow(int[][] arr, int m)
         
           that returns the sum of the elements in row m.
         * 
           Assume the value of m satisfies 0 <= m < arr.length
         */
        public static int sumRow(int[][] arr, int m){
            int sum = 0;
            for (m = 0; m < arr.length; m++){
                for (int num = 0; num < arr[m].length; num++){
                    sum = sum + arr[m][num];
    
                }
            }
            return sum;
        }
        
        /* Step 3:  Write a method with the header
         
           public static int sumCol(int[][] arr, int n)
         
           that returns the sum of the elements in col n
         
           Assume (i) every row contains the same number of columns and (ii)
           the value of n satisfies 0 <= n < arr[0].length
         */
        
        /* Suggestion:  Before writing sumRow and sumCol, review the 
           computeSum method in the notes for Lecture 18.
         */
        public static int sumCol(int[][] arr, int n){
            int sum = 0;
            for (int num = 0; num < n; num++){
                for (int num2 = 0; num2 < n; num2++){
                    sum = sum + arr[num][num2];
    
                }
            }
            return sum;
        }
        
         
        public static void main(String[] args)
        {
            final int ROWS = 40;
            final int COLS = 15;
            Scanner in = new Scanner(System.in);
            int[][] data = new int[40][15];
            int row;
            int col;
            
            for(int j = 0; j < data.length; j++)
                for(int k = 0; k < data[j].length; k++)
                    data[j][k] = (int)( (j + k)*(Math.random() + 1));
            
            System.out.println("Enter an integer from 0 to " + (ROWS - 1));
            row = in.nextInt();
            System.out.println("Enter an integer from 0 to " + (COLS- 1));
            col = in.nextInt();
    
    
            System.out.println("The sum of the elements in row " + row +
                               " is " + sumRow(data, row));
            System.out.println("The sum of the elements in row " + row +
                               " is " + sumCol(data, col));
        }
        
    }

  2. #2
    coasterguy10 is offline Member
    Join Date
    Sep 2011
    Posts
    21
    Rep Power
    0

    Default Re: 2d Array Sums Help

    For finding the sum of the rows you want to keep row m the same and only add up what is in that row. The code you have now is set to a loop that will add the element of every index to the sum. To fix the problem, delete your first for loop, "for (m = 0; m < arr.length; m++)". This way row m will stay constant and the loop will not add elements from other rows. Your sum should just be sum+= arr[m][num]. The same goes for adding the elements in the specified column. Right now your loop would not work correctly even if you wanted to add everything in the array because of your second for loop. Your parameters state that num2 must be less then n for the loop to continue, but what if n was 0? The loop wouldn't even start because 0 is not less then 0 (I know this sounds patronizing). I would just take out the second for loop and keep your variables as num and n. Then change the for loop to read, "for (int num = 0; num < arr[n].length; num++)". Then your sum should be written as sum+= arr[num][n]; You put n in the second set of braces because that is what determines the column of the array. By keeping n the same for eveything the program sums, once it adds all the elements in column n, the loop will stop. The same for why m goes in the first set of braces for adding elements in the row. And make sure you import Scanner at the top of your program so you can read in the input. Hope this helps and makes sense.

  3. #3
    XxVashxX is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default Re: 2d Array Sums Help

    Quote Originally Posted by coasterguy10 View Post
    For finding the sum of the rows you want to keep row m the same and only add up what is in that row. The code you have now is set to a loop that will add the element of every index to the sum. To fix the problem, delete your first for loop, "for (m = 0; m < arr.length; m++)". This way row m will stay constant and the loop will not add elements from other rows. Your sum should just be sum+= arr[m][num]. The same goes for adding the elements in the specified column. Right now your loop would not work correctly even if you wanted to add everything in the array because of your second for loop. Your parameters state that num2 must be less then n for the loop to continue, but what if n was 0? The loop wouldn't even start because 0 is not less then 0 (I know this sounds patronizing). I would just take out the second for loop and keep your variables as num and n. Then change the for loop to read, "for (int num = 0; num < arr[n].length; num++)". Then your sum should be written as sum+= arr[num][n]; You put n in the second set of braces because that is what determines the column of the array. By keeping n the same for eveything the program sums, once it adds all the elements in column n, the loop will stop. The same for why m goes in the first set of braces for adding elements in the row. And make sure you import Scanner at the top of your program so you can read in the input. Hope this helps and makes sense.
    Aha, now it makes complete sense. Thanks alot man. Seems I was over analyzing the entire thing.

  4. #4
    coasterguy10 is offline Member
    Join Date
    Sep 2011
    Posts
    21
    Rep Power
    0

    Default Re: 2d Array Sums Help

    Quote Originally Posted by XxVashxX View Post
    Aha, now it makes complete sense. Thanks alot man. Seems I was over analyzing the entire thing.
    No problem, glad I could help

Similar Threads

  1. Replies: 0
    Last Post: 05-12-2011, 07:04 PM
  2. Harmonic sums - Recursive
    By überfuzz in forum New To Java
    Replies: 4
    Last Post: 03-25-2011, 10:42 AM
  3. Replies: 23
    Last Post: 09-07-2010, 08:12 PM
  4. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  5. Problem with Sums and Averages in Sales Report
    By DavidEvans in forum New To Java
    Replies: 9
    Last Post: 04-21-2010, 08:57 PM

Posting Permissions

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