Home > Software engineering >  Swapping columns of the Matrix
Swapping columns of the Matrix

Time:04-22

I made this code in order to swap places with the first column and the last column in a 3x3 Matrix. I would need to use 2 different methods, one to print the original matrix and one to print the modified matrix.

I was able to do the first one, but I have problems with the second.

public class SwapMatrix {    
  
    public static void main(String[] args) {

        int matrix [][] = 
            {{3, 4, 5}, 
            {7, 8, 9}, 
            {1, 2, 3}};
        System.out.println("\n Normal Matrix:");
        print(matrix);
        
        
        //int matrixModified [][] = swap(matrix);
        //System.out.println("\n New Matrix:");
        //print(matrixModified);
    }
    
    public static void print (int matrix[][]){
        for (int i=0; i<matriz.length; i  ){
           for (int j=0; j<matrix[0].length; j  ){
               System.out.print(matriz [i][j]  "\t");
           }
           System.out.println();
        }
    }
}  

CodePudding user response:

If you want to print the matrix backwards, all you have to do is to count reverse in your loop.

public static void printReverse (int matrix[][]){
    for (int i=0; i<matrix.length; i  ){             // lines
       for (int j=matrix[0].length-1; j>=0; j--){    // columns backwards
           System.out.print(matrix [i][j]  "\t");
       }
       System.out.println();
    }
}

CodePudding user response:

You can implement the swap() method in two ways: either by creating a new matrix or by swapping the columns of the given matrix in place.

1. Firstly, you need to create a new matrix.

And then iterate over it with a nested for loop assigning the elements of each row in the new matrix in reversed order. I.e. the value of the first element of the row at position 0 would be assigned to the element at position matrix[0].length - 1 and vice versa (the same hold true for all other elements).

public static int[][] swap(int[][] matrix) {
    int[][] result = new int[matrix.length][matrix[0].length];
    for (int row = 0; row < matrix.length; row  ) {
        for (int col = 0; col < matrix[0].length; col  ) {
            result[row][(matrix[0].length - 1) - col] = matrix[row][col];
        }
    }
    return result;
}

2. To swap the given matrix in place, you need to create a nested for loop. Iteration in the inner loop should happen only until the middle of the array representing each row (that's important, if'll iterate over the full length of a row elements will get swapped twice and the matrix will remain the same).

public static int[][] swapInPlace(int[][] matrix) {
    for (int row = 0; row < matrix.length; row  ) {
        for (int col = 0; col < matrix[0].length / 2; col  ) {
            int temp = matrix[row][(matrix[0].length - 1) - col];
            matrix[row][(matrix[0].length - 1) - col] = matrix[row][col];
            matrix[row][col] = temp;
        }
    }
    return matrix;
}
  • Related