Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Exercise7_1 {
- public static double sumColumn(double[][] m, int columnIndex) {
- double sum = 0;
- for (int i = 0; i < m.length; i++) {
- sum += m[i][columnIndex];
- }
- return sum;
- }
- public static void main(String[] args) {
- double[][] matrix = new double[3][4];
- String[][] rows = new String[3][4];
- Scanner in = new Scanner(System.in);
- System.out.println("Enter a 3-by-4 matrix row by row:");
- rows[0] = in.nextLine().split(" ");
- rows[1] = in.nextLine().split(" ");
- rows[2] = in.nextLine().split(" ");
- for (int i = 0; i < rows.length; i++) {
- for (var j = 0; j < rows[i].length; j++) {
- matrix[i][j] = Double.parseDouble(rows[i][j]);
- }
- }
- for (int i = 0; i < 4; i++) {
- double sum = sumColumn(matrix, i);
- System.out.println("Sum of the elements at column " + i + " is " + sum);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement