-
Notifications
You must be signed in to change notification settings - Fork 2
/
Operations_on_Matrix.java
57 lines (52 loc) · 1.78 KB
/
Operations_on_Matrix.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.company;
import java.util.Scanner;
public class Operations_on_Matrix {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int rows1 = s.nextInt(), cols1 = s.nextInt();
int[][] matrix1 = new int[rows1][cols1];
int[][] matrix2 = new int[rows1][cols1];
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols1; j++) {
matrix1[i][j] = s.nextInt();
}
}
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols1; j++) {
matrix2[i][j] = s.nextInt();
}
}
add(matrix1, matrix2, rows1, cols1);
multiply(matrix1, matrix2, rows1, cols1);
}
private static void multiply(int[][] matrix1, int[][] matrix2, int row, int col) {
int[][] result = new int[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
for (int k = 0; k < row; k++){
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
System.out.print(result[i][j] + " ");
}
System.out.println();
}
}
private static void add(int[][] matrix1, int[][] matrix2, int row, int col) {
int[][] result = new int[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
result[i][j] += matrix1[i][j] + matrix2[i][j];
}
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
System.out.print(result[i][j] + " ");
}
System.out.println();
}
}
}