-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise08_05.java
More file actions
50 lines (42 loc) · 1.82 KB
/
Copy pathExercise08_05.java
File metadata and controls
50 lines (42 loc) · 1.82 KB
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
44
45
46
47
48
49
50
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_05 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double[][] a = new double[3][3];
double[][] b = new double[a.length][a[0].length];
System.out.println("Enter matrix1");
for (int row = 0; row < a.length; row++) {
for (int col = 0; col < a[row].length; col++) {
a[row][col] = input.nextDouble();
}
}
System.out.println("Enter matrix2");
for (int row = 0; row < b.length; row++) {
for (int col = 0; col < b[row].length; col++) {
b[row][col] = input.nextDouble();
}
}
double[][] c = addMatrix(a, b);
System.out.printf(" %.1f %.1f %.1f %.1f %.1f %.1f %.1f %.1f %.1f \n", a[0][0], a[0][1], a[0][2], b[0][0], b[0][1], b[0][2], c[0][0], c[0][1], c[0][2]);
System.out.printf(" %.1f %.1f %.1f + %.1f %.1f %.1f = %.1f %.1f %.1f \n", a[1][0], a[1][1], a[1][2], b[1][0], b[1][1], b[1][2], c[1][0], c[1][1], c[1][2]);
System.out.printf(" %.1f %.1f %.1f %.1f %.1f %.1f %.1f %.1f %.1f \n", a[2][0], a[2][1], a[2][2], b[2][0], b[2][1], b[2][2], c[2][0], c[2][1], c[2][2]);
}
public static double[][] addMatrix(double[][] a, double[][] b){
double[][] c = new double[a.length][a[0].length];
for (int row = 0; row < a.length; row++) {
for (int col = 0; col < b.length; col++) {
c[row][col] = a[row][col] + b[row][col];
}
}
return c;
}
}