Results 1 to 3 of 3
Thread: matrix task in java
 05082013, 11:12 PM #1Member
 Join Date
 May 2013
 Posts
 4
 Rep Power
 0
matrix task in java
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; 05082013 at 11:28 PM.
 05082013, 11:42 PM #2Senior Member
 Join Date
 Nov 2012
 Posts
 257
 Rep Power
 3
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;
 05092013, 08:25 AM #3
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,945
 Blog Entries
 7
 Rep Power
 22
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,
JosI have the stamina of a seal; I lie on the beach instead of running on it.
Similar Threads

Knapsack problem , check if matrix can fill list of smaller matrix list.
By ofirattia in forum Advanced JavaReplies: 1Last Post: 12082012, 08:20 PM 
Java Task with matrix.
By vitaliy1901 in forum New To JavaReplies: 5Last Post: 10252012, 04:23 PM 
Help with a Java Task!
By DABBISH in forum New To JavaReplies: 7Last Post: 09042011, 04:14 PM 
JAVA task.
By DEA in forum New To JavaReplies: 2Last Post: 04082010, 09:39 PM 
javatask.com
By marcellis in forum IntroductionsReplies: 0Last Post: 09282009, 10:46 AM
Bookmarks