-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathExercise08_01.java
43 lines (32 loc) · 1.27 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
38
39
40
41
42
43
package ch_08;
/* 8.1 (Sum elements column by column) Write a method that returns the sum of
* all the elements in a specified column in a matrix using the following header:
* public static double sumColumn(double[][] m, int columnIndex)
* Write a test program that reads a 3-by-4
* matrix and displays the sum of each column.
*/
import java.util.Scanner;
public class Exercise08_01 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double[][] superArray = new double[3][4];
System.out.println("Enter a 3 by 4 matrix row by row: ");
for(int i = 0; i < superArray.length; i++ ) {
for(int j = 0; j < superArray[i].length; j++) {
superArray[i][j] = input.nextDouble();
}
}
System.out.println("The sum of the elements in 0 is " + sumColumn(superArray,0));
System.out.println("The sum of the elements in 1 is " + sumColumn(superArray,1));
System.out.println("The sum of the elements in 2 is " + sumColumn(superArray,2));
System.out.println("The sum of the elements in 3 is " + sumColumn(superArray,3));
input.close();
}
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;
}
}