Home > Net >  java 2d array/matrix got to rotate with user input
java 2d array/matrix got to rotate with user input

Time:11-16

I have a problem with creating a matrix with user input 4x3 and rotate it 90 degrees. Anyone knows what my next step could be?

First i tried to solve the rotating and now i cant get it to work with a scanner..

public static void main(String[] args) {

    Scanner sca = new Scanner(System.in);
    int[][] matrix = new int[4][4];
    System.out.println("Enter "   matrix.length   " rows and "   matrix[0].length   " columns: ");
    matrix= sca.nextInt();

    int N = matrix.length;
    for (int i = 0; i < N; i  ) {

        for (int j = 1; j < N; j  ) {

            int temp = matrix[i][j];
            matrix[i][j] = matrix[j][i];
            matrix[j][i] = temp;
        }
    }
    for (int i = 0; i < N; i  ) {
        for (int j = 0; j < (N / 2); j  ) {
            int temp = matrix[i][j];
            matrix[i][j] = matrix[i][N - 1 - j];
            matrix[i][N - 1 - j] = temp;
        }

    }


}

}

CodePudding user response:

Your wont work if you a n*m matrix(non square matrix). We have to use an extra space

public void rotate(int[][] matrix) {
    
    int A[][] = new int[matrix.length][matrix[0].length];
    for(int i=0; i<matrix.length; i  ){
        for(int j=0; j<matrix[i].length; j  ){
            A[i][j] = matrix[j][i];
        }   
    }
    
    for(int i=0; i<matrix.length; i  ){
        for(int j=0; j<matrix[i].length; j  ){
            matrix[i][j] = A[i][A[i].length-1-j];
        }   
    }

I hope this helps to answer your questions.

CodePudding user response:

I think that you are overthinking when you say "rotate" your matrix.

I just made an example with user input and it does seem Ok, let me know if this helps you:

public void test() {

        final Scanner scanner = new Scanner(System.in);

        System.out.println("Enter the matrix height (number of rows):");

        int height = scanner.nextInt();

        System.out.println("Enter the matrix width  (number of columns):");

        int width = scanner.nextInt();
        
        int[][] matrix = new int[height][width];

        System.out.println("Now, enter the matrix content.");

        for (int i = 0; i < height; i  ) {

            for (int j = 0; j < width; j  ) {

                System.out.print(String.format("Enter the value to cell in the point %d, %d", i, j));

                matrix[i][j] = scanner.nextInt();

            }

            System.out.println();

        }

        print(matrix);

        
        scanner.close();
    }

    void print(final int[][] array) {
        for (int i = 0; i < array.length; i  ) {
            for (int j = 0; j < array[i].length; j  ) {
                System.out.print(array[i][j]   ",");
            }
            System.out.println();
        }
    }



   void rotate(int[][] array) {
        int[][] other = new int[array[0].length][array.length];

        for (int i = 0; i < array.length; i  ) {
            for (int j = 0; j < array[i].length; j  ) {

                other[j][i] = array[i][j];
            }
        }

        print(other);

    }

First you ask for the number of rows, then columns.

Second, you have to ask for values in each cell (in the firt loop is what i do).

Third, i print the content of the matrix with the print method.

Fourth, i send the matrix to a rotate method, in the body of the rotate method, i sent the second matrix to print.

Finally, close the scanner.

I test it with the next data:

1,2,3,4
5,6,7,8
9,10,11,12,

It showed me the next conversion:

1,5,9
2,6,10
3,7,11
4,8,12

I hope this is the desired result.

Cheers.

  • Related