Skip to Main Content

Java Development Tools

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Inverse of Matrix problem

1007887May 8 2013 — edited May 9 2013
Hello.

I have a inverse matrix task.Teacher said us that we have to create little a program which is finds inverse of the matrix which is given in code.


I did something like this and when I gave him this task he said me that "I want the program calculate itself matrix size.You wont give size of matrix like int[4][4]; this.You just will write the matrix in your code and the program will change as depend to size and calculate automatically as the matrix."

I some more explain for all of you understand more.
Example I write in code
3x3 a matrix okay.Below an example.

3 4 6
1 5 7
1 3 7

This is 3x3 matrix so its calculating.And I want now 4x4 matrix.

4 5 1 6
1 4 5 6
2 4 5 5
1 2 4 7

Also this matrix will calculate as automatically depend to matrix size.

I musnt write [4][4] or [3][3].Instead this I need to write something like will change as matrix size automatically.I hope you understand.Because Im really bored with this.Its just second month in programming for us and I have no idea about this task anymore.


import java.util.Scanner;
public class inversematrix {
public static void main(String[] args)
{
int[][] matrix = new int[4][4];
Scanner inputUser = new Scanner(System.in);
System.out.println("Write matrix number");
for(int row=0; row<4; row++)
{
for(int column=0; column<4; column++)
{
System.out.print("A"+(row+1)+(column+1)+" = ");
matrix[row][column] = inputUser.nextInt();
}
}

System.out.println("4 na 4 matris");
for(int row=0; row<3; row++)
{
for(int column=0; column<3; column++)
{
System.out.printf("%3d",matrix[row][column]);
}
System.out.print("\n");
}

System.out.println("Inverse of matrix calculating");
int matrixInvers[][] = {{matrix[1][1]*matrix[2][2]-matrix[1][2]*matrix[2][1],matrix[0][2]*matrix[2][1]-matrix[0][1]*matrix[2][2],matrix[0][1]*matrix[1][2]-matrix[0][2]*matrix[1][1]},
{matrix[1][2]*matrix[2][0]-matrix[1][0]*matrix[2][2],matrix[0][0]*matrix[2][2]-matrix[0][2]*matrix[2][0],matrix[0][2]*matrix[1][0]-matrix[0][0]*matrix[1][2]},
{matrix[1][0]*matrix[2][1]-matrix[1][1]*matrix[2][0],matrix[0][2]*matrix[2][0]-matrix[0][0]*matrix[2][1],matrix[0][0]*matrix[1][1]-matrix[0][1]*matrix[1][0]}};

System.out.println("Inverse matrix");
for(int row=0; row<3; row++)
{
for(int column=0; column<3; column++)
{
System.out.printf("%3d",matrixInvers[row][column]);
}
System.out.print("\n");
}
}
}

Edited by: 1004884 on 08.May.2013 14:27
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 6 2013
Added on May 8 2013
3 comments
1,564 views