# 2d Array Sums Help

• 10-17-2011, 04:48 AM
XxVashxX
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. :(grin):

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));     }     }```
• 10-17-2011, 06:37 AM
coasterguy10
Re: 2d Array Sums Help
• 10-17-2011, 06:47 AM
XxVashxX
Re: 2d Array Sums Help
Quote:

Originally Posted by coasterguy10