I am newbie to Java. Here is my scenario,
I need to read the multiple square matrix at once and process the same elements of the matrix to find the inverse. I am able to process a single matrix. How can I convert this code to read multiple square matrices and produce the expected result.
import java.util.Scanner;
public class Matrix {
public static void main(String args[]) {
int i, j;
int determinant, temp;
int matrix[][] = new int[2][2];
Scanner sc = new Scanner(System.in);
for(i = 0; i < 2; ++i)
for(j = 0; j < 2; ++j)
matrix[i][j] = sc.nextInt();
System.out.print("\n");
determinant = (matrix[0][0] * matrix[1][1]) - (matrix[0][1] * matrix[1][0]);
temp = matrix[0][0];
matrix[0][0] = matrix[1][1];
matrix[1][1] = temp;
matrix[0][1] = - matrix[0][1];
matrix[1][0] = - matrix[1][0];
System.out.println("\nMatrix 1:");
for(i = 0; i < 2; ++i) {
for(j = 0; j < 2; ++j) {
System.out.print((matrix[i][j]/determinant) + " ");
}
System.out.print("\n");
}
}
}
I am able to read and process the single input.
Input:
------
1 0
0 1
3 7
1 4
output:
------
Matrix 1:
1 0
0 1
Matrix2:
0 -1
0 0