Results 1 to 3 of 3
  1. #1
    mockingbird is offline Member
    Join Date
    May 2013
    Rep Power

    Default matrix task in java


    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.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.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]},

    System.out.println("Inverse matrix");
    for(int row=0; row<3; row++)
    for(int column=0; column<3; column++)
    Last edited by mockingbird; 05-08-2013 at 11:28 PM.

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Rep Power

    Default 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:


    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. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power

    Default 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,

    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Replies: 1
    Last Post: 12-08-2012, 07:20 PM
  2. Java Task with matrix.
    By vitaliy1901 in forum New To Java
    Replies: 5
    Last Post: 10-25-2012, 04:23 PM
  3. Help with a Java Task!
    By DABBISH in forum New To Java
    Replies: 7
    Last Post: 09-04-2011, 04:14 PM
  4. JAVA task.
    By DEA in forum New To Java
    Replies: 2
    Last Post: 04-08-2010, 09:39 PM
    By marcellis in forum Introductions
    Replies: 0
    Last Post: 09-28-2009, 10:46 AM

Posting Permissions

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