Greta
Greta

Reputation: 328

Swap two columns of a 2D-matrix

I have given a two-dimensional array (matrix) and the two numbers: i and j. My goal is to swap the columns with indices i and j within the matrix. Input contains matrix dimensions n and m, not exceeding 100, then the elements of the matrix, then the indices i and j.

I guess the origin of the problem has something to do with referenced variables? I tried to replace line 15 with

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

but still the output remains the same...

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int row = scanner.nextInt();
        int column = scanner.nextInt();

        int[][] matrix = new int[row][column];
        int[][] swap = matrix.clone();

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                matrix[i][j] = scanner.nextInt();
            }
        }

        int c0 = scanner.nextInt();
        int c1 = scanner.nextInt();

        for (int i = 0; i < row; i++) {
            swap[i][c0] = matrix[i][c1];
            swap[i][c1] = matrix[i][c0];
        }

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                System.out.print(swap[i][j] + " ");
            }
            System.out.println();
        }

    }
}

My Input:

3 4 
11 12 13 14 
21 22 23 24 
31 32 33 34 
0 1

3 and 4 stands for the number of rows and columns of the matrix, the following three lines define the elements of the matrix and the last line tells the program which columns so swap.

Expected Output:

12 11 13 14 
22 21 23 24 
32 31 33 34 

Actual Output:

12 12 13 14 
22 22 23 24 
32 32 33 34

Upvotes: 0

Views: 2023

Answers (3)

Monu Rohilla
Monu Rohilla

Reputation: 665

in your program, a logical error is their : see my code

import javax.sound.sampled.Line;
import java.util.Arrays;
import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // put your code here
        int i = scanner.nextInt();
        int j = scanner.nextInt();
        int[][] matrix = new int[i][j];
        for (int y = 0; y < i; y++) {
            for (int x = 0; x < j; x++) {
                matrix[y][x] = scanner.nextInt();
            }
        }
        int swap1 = scanner.nextInt();
        int swap2 = scanner.nextInt();
        for (int[] arr : matrix) {
            int temp = arr[swap1];
            arr[swap1] = arr[swap2];
            arr[swap2] = temp;
            for (int el : arr) {
                System.out.print(el + " ");
            }
            System.out.println();
        }
    }
}

Upvotes: 0

Greta
Greta

Reputation: 328

Thanks a lot @Tim Biegeleisen!

This code worked for me:

import java.util.Scanner;

public class Main {

  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int row = scanner.nextInt();
    int column = scanner.nextInt();

    int [][] matrix = new int[row][column];
    int [][] swap = matrix.clone();

    for (int i = 0; i < row; i++) {
      for (int j = 0; j < column; j++) {
        matrix[i][j] = scanner.nextInt();}}

    int c0 = scanner.nextInt();
    int c1 = scanner.nextInt();

    for (int i=0; i < row; i++) {
      int temp = matrix[i][c0];
      matrix[i][c0] = matrix[i][c1];
      matrix[i][c1] = temp;
    }

    for (int i = 0; i < row; i++) {
      for (int j = 0; j < column; j++) {
        System.out.print(swap[i][j]+" "); }
      System.out.println();}

  }
}

Upvotes: 1

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520938

It looks like your swapping logic is off. If you want to swap two variables, say a and b, then here is a pattern which works in Java:

int a = 5;
int b = 10;
int temp = a;
a = b;
b = temp;

Applying this logic to your matrix column swap, we can try the following updated code:

int c0 = scanner.nextInt();
int c1 = scanner.nextInt();

for (int i=0; i < row; i++) {
    int temp = matrix[i][c0];
    matrix[i][c0] = matrix[i][c1];
    matrix[i][c1] = temp;
}

Upvotes: 1

Related Questions