Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class test {
- public static void main(String[] args) {
- 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}};
- System.out.println("The sum of all numbers in the array is: " + getTotal(array2D));
- System.out.println("The average of all numbers in the array is: " + getAverage(array2D));
- for (int i = 0; i < array2D.length; i++)
- {
- System.out.println("Sum of row " + (i+1) +": " + getRowTotal(array2D, i));
- }
- System.out.println();
- for (int i = 0; i < array2D[0].length; i++)
- {
- System.out.println("Sum of column " + (i+1) + ": " + getColTotal(array2D, i));
- }
- System.out.println();
- for (int i = 0; i < array2D.length; i++)
- {
- System.out.println("Highest in row " + (i+1) + ": " + getHighestInRow(array2D, i));
- }
- System.out.println();
- for (int i = 0; i < array2D.length; i++)
- {
- System.out.println("Lowest in row " + (i+1) + ": " + getLowestInRow(array2D, i));
- }
- }
- public static double getTotal(double[][] arry)
- {
- double total = 0;
- for (int row = 0; row < arry.length; row ++)
- {
- for (int col = 0; col < arry[row].length; col++)
- {
- total += arry[row][col];
- }
- }
- return total;
- }
- public static double getAverage(double[][] arry)
- {
- double total = 0;
- for (int col = 0; col < arry.length; col++)
- {
- for (int row = 0; row < arry[0].length; row++)
- {
- total += 1;
- }
- }
- return getTotal(arry) / total;
- }
- public static double getRowTotal(double[][] arry, int row)
- {
- double total = 0;
- for (int col = 0; col < arry[0].length; col++)
- {
- total += arry[row][col];
- }
- return total;
- }
- public static double getColTotal(double[][] arry, int col)
- {
- double total = 0;
- for (int row = 0; row < arry.length; row++)
- {
- total += arry[row][col];
- }
- return total;
- }
- public static double getHighestInRow(double[][] arry, int row)
- {
- double highest = arry[row][0];
- for (int col = 1; col < arry[row].length; col++)
- {
- if (arry[row][col] > highest)
- {
- highest = arry[row][col];
- }
- }
- return highest;
- }
- public static double getLowestInRow(double[][] arry, int row)
- {
- double lowest = arry[row][0];
- for (int col = 0; col < arry[row].length; col++)
- {
- if (arry[row][col] < lowest)
- {
- lowest = arry[row][col];
- }
- }
- return lowest;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement