-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewClass.java
67 lines (56 loc) · 2.03 KB
/
NewClass.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
58
59
60
61
62
63
64
65
66
67
import java.util.Scanner;
public class NewClass {
public static int[][] inputMatrix(int nr, int nc) {
Scanner input = new Scanner(System.in);
System.out.print("Enter num of rows: ");
nr = input.nextInt();
System.out.print("Enter num of cols: ");
nc = input.nextInt();
int[][] matrix = new int[nr][nc];
System.out.println("Enter matrix elements: ");
for (int k = 0; k < nr; k++) {
for (int k2 = 0; k2 < nc; k2++) {
matrix[k][k2] = input.nextInt();
}
}
return matrix;
}
static void displayMatrix(int nr, int nc, int[][] matrix) {
System.out.println("Elements of input matrix are:");
for (int k2 = 0; k2 < nr; k2++) {
for (int l = 0; l < nc; l++) {
System.out.print(matrix[k2][l] + " ");
}
System.out.println();
}
}
public static int[][] makePivotOne(int[][] matrix, int pivotRow, int pivotColumn) {
int nc = matrix.length;
int pivotElement = matrix[pivotRow][pivotColumn];
for (int i = 0; i < nc; i++) {
matrix[pivotRow][i] = matrix[pivotRow][i] / pivotElement;
}
return matrix;
}
public static int[][] makePivotColZero(int[][] matrix, int pivotRow, int pivotColumn, int pivotValue) {
int nr = matrix.length;
int nc = matrix.length;
for (int r = 0; r < nr; r++) {
if (r == pivotRow) {
continue;
}
if (r != pivotRow) {
pivotValue = matrix[r][pivotColumn];
}
for (int i = 0; i < nc; i++) {
matrix[r][i] = matrix[r][i] - matrix[pivotRow][i] * pivotValue;
}
}
return matrix;
}
public static void main(String[] args) {
int[][] abc_matrix = NewClass.inputMatrix(2, 2);
NewClass.displayMatrix(2, 2, abc_matrix);
// Display matrix
}
}