1

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
mate00
  • 2,727
  • 5
  • 26
  • 34
  • 1
    use a loop and reuse the intermediate results. – UninformedUser Apr 30 '19 at 09:13
  • Hello ! I suggest you to read [the guideline](https://stackoverflow.com/help/how-to-ask) about how to ask your question. You just copy/paste your code, and didn't tell what you did and what is your specific problem. The purpose of stackoverflow is not to do your job, but to help to solve specific problems. you can check [this question](https://stackoverflow.com/questions/1992638/java-inverse-matrix-calculation) about matrix inversion in java. – NayoR Apr 30 '19 at 09:37

0 Answers0