Advertisement
fosterbl

TotalRow.java SOLUTION

Jan 22nd, 2020
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.84 KB | None | 0 0
  1. //(c) A+ Computer Science
  2. //www.apluscompsci.com
  3. //Name -
  4. import java.util.Arrays;
  5.  
  6. public class TotalRow
  7. {
  8.     public static int[] getRowTotals( int[][] m )
  9.     {
  10.         int[] rowTotals = new int[m.length];
  11.       for(int r = 0; r < m.length; r++){
  12.          int rowTotal = 0;
  13.          for(int c = 0; c < m[r].length; c++){
  14.             rowTotal += m[r][c];
  15.          }
  16.          rowTotals[r] = rowTotal;
  17.       }
  18.       return rowTotals;
  19.     }
  20.      
  21.      public static void main(String[] args){
  22.         int[][] f = {{1,2,3},{5,5,5,5}};
  23.         System.out.println( Arrays.toString( getRowTotals( f ) ) );
  24.        
  25.         f = new int[][]{{1,2,3},{5},{1},{2,2}};
  26.         System.out.println( Arrays.toString( getRowTotals( f ) ) );
  27.        
  28.         f = new int[][]{{1,2},{5,5},{5,5},{4,5,6,7},{123124,12312}};
  29.         System.out.println( Arrays.toString( getRowTotals( f ) ) );
  30.        
  31.      }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement