Advertisement
Nick-O-Rama

2dArray

Jan 21st, 2015
515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.58 KB | None | 0 0
  1.  
  2. public class test {
  3.  
  4.     public static void main(String[] args) {
  5.         double[][] array2D = {{2.1, 6.2, 3.1, 5.0}, {1.5, 1.6, 1.9, 5.0}, {3.1, 3.2, 2.4, 4.0}};
  6.         System.out.println("The sum of all numbers in the array is: " + getTotal(array2D));
  7.         System.out.println("The average of all numbers in the array is: " + getAverage(array2D));
  8.         for (int i = 0; i < array2D.length; i++)
  9.         {
  10.             System.out.println("Sum of row " + (i+1) +": " + getRowTotal(array2D, i));
  11.         }
  12.         System.out.println();
  13.        
  14.         for (int i = 0; i < array2D[0].length; i++)
  15.         {
  16.             System.out.println("Sum of column " + (i+1) + ": " + getColTotal(array2D, i));
  17.         }
  18.         System.out.println();
  19.        
  20.         for (int i = 0; i < array2D.length; i++)
  21.         {
  22.             System.out.println("Highest in row " + (i+1) + ": " + getHighestInRow(array2D, i));
  23.         }
  24.         System.out.println();
  25.        
  26.         for (int i = 0; i < array2D.length; i++)
  27.         {
  28.             System.out.println("Lowest in row " + (i+1) + ": " + getLowestInRow(array2D, i));
  29.         }
  30.  
  31.     }
  32.    
  33.     public static double getTotal(double[][] arry)
  34.     {
  35.         double total = 0;
  36.         for (int row = 0; row < arry.length; row ++)
  37.         {
  38.             for (int col = 0; col < arry[row].length; col++)
  39.             {
  40.                 total += arry[row][col];
  41.             }
  42.         }
  43.         return total;
  44.     }
  45.    
  46.     public static double getAverage(double[][] arry)
  47.     {
  48.         double total = 0;
  49.         for (int col = 0; col < arry.length; col++)
  50.         {
  51.             for (int row = 0; row < arry[0].length; row++)
  52.             {
  53.                 total += 1;
  54.             }
  55.         }
  56.         return getTotal(arry) / total;
  57.     }
  58.    
  59.     public static double getRowTotal(double[][] arry, int row)
  60.     {
  61.         double total = 0;
  62.         for (int col = 0; col < arry[0].length; col++)
  63.         {
  64.             total += arry[row][col];
  65.         }
  66.     return total;
  67.     }
  68.    
  69.     public static double getColTotal(double[][] arry, int col)
  70.     {
  71.         double total = 0;
  72.         for (int row = 0; row < arry.length; row++)
  73.         {
  74.             total += arry[row][col];
  75.         }
  76.     return total;
  77.     }
  78.    
  79.     public static double getHighestInRow(double[][] arry, int row)
  80.     {
  81.         double highest = arry[row][0];
  82.         for (int col = 1; col < arry[row].length; col++)
  83.         {
  84.             if (arry[row][col] > highest)
  85.             {
  86.                 highest = arry[row][col];
  87.             }
  88.         }
  89.     return highest;
  90.     }
  91.    
  92.     public static double getLowestInRow(double[][] arry, int row)
  93.     {
  94.         double lowest = arry[row][0];
  95.         for (int col = 0; col < arry[row].length; col++)
  96.         {
  97.             if (arry[row][col] < lowest)
  98.             {
  99.                 lowest = arry[row][col];
  100.             }
  101.         }
  102.     return lowest;
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement