1. Member
Join Date
May 2013
Posts
4
Rep Power
0

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.

Here codes.

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");
}
}
}
Last edited by mockingbird; 05-08-2013 at 11:28 PM.

2. Senior Member
Join Date
Nov 2012
Posts
257
Rep Power
5

## Re: matrix task in java

You need to a way to determine a new row in your matrix otherwise you scanner would just allow you to add numbers over and over.

before i eneter a number i would ask: "How big will this matrix be" if the user enters 5 then create an array[5][5] but it will read

int size = scanner.nextInt();

then add each letter typed to the array using nested loops which depend on the value entered for the size.

once you have completed a row start adding to the next row in the array. This can be done using nested loops:

e.g

Java Code:
```for(int x = 0; x < intInputted ; x++) {
for(int y = 0; y < intInputted; y++) {
int i = scanner.nextInt();
array[x][y] = i;```
Something like that.

3. ## Re: matrix task in java

There is no need to explicitly ask for the dimension of the matrix: if the first line contains n numbers you immediately know that the matrix has n rows and n columns; a few tips: use doubles for the matrix elements instead of ints and don't blindly use a 3x3 inverse calculation; also guard your code agains a singular matrix (it doesn't have an inverse).

kind regards,

Jos

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•