Advertisement
PadmaJS

Exercise 7.1

Sep 30th, 2022
1,082
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 0.92 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Exercise7_1 {
  4.   public static double sumColumn(double[][] m, int columnIndex) {
  5.     double sum = 0;
  6.     for (int i = 0; i < m.length; i++) {
  7.       sum += m[i][columnIndex];
  8.     }
  9.     return sum;
  10.   }
  11.  
  12.   public static void main(String[] args) {
  13.     double[][] matrix = new double[3][4];
  14.     String[][] rows = new String[3][4];
  15.     Scanner in = new Scanner(System.in);
  16.     System.out.println("Enter a 3-by-4 matrix row by row:");
  17.     rows[0] = in.nextLine().split(" ");
  18.     rows[1] = in.nextLine().split(" ");
  19.     rows[2] = in.nextLine().split(" ");
  20.     for (int i = 0; i < rows.length; i++) {
  21.       for (var j = 0; j < rows[i].length; j++) {
  22.         matrix[i][j] = Double.parseDouble(rows[i][j]);
  23.       }
  24.     }
  25.     for (int i = 0; i < 4; i++) {
  26.       double sum = sumColumn(matrix, i);
  27.       System.out.println("Sum of the elements at column " + i + " is " + sum);
  28.     }
  29.   }
  30. }
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement