-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExercise08_01.java
37 lines (32 loc) · 1.06 KB
/
Exercise08_01.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.util.Scanner;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author jorda
*/
public class Exercise08_01 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a 3 by 4 matrix row by row: ");
double[][] array = new double[3][4];
for (int row = 0; row < array.length; row++) {
for (int col = 0; col < array[row].length; col++) {
array[row][col] = input.nextDouble();
}
}
for (int col = 0; col < array[0].length; col++) {
System.out.println("Sum of the elements at column " + col + " is " + sumColumn(array, col));
}
}
public static double sumColumn(double[][] m, int columnIndex) {
double total = 0;
for (int row = 0; row < m.length; row++) {
total += m[row][columnIndex];
}
return total;
}
}