87.2k views
2 votes
(Shuffle rows) Write a method that shuffles the rows in a two-dimensional int array using the following header: public static void shuffle(int[][] m)

User Azarro
by
5.3k points

1 Answer

4 votes

The question is incomplete. The complete question is :

Write a method that shuffles the rows in a two-dimensional int array using the following header: public static void shuffle(int[][] m)

Write a test program that shuffles the following matrix: int[][] m = {{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}};

Solution :

//filename_suffle.java

#public class shuffle{

public_static_void_main_(String[] args) {

int[][] m = {{1, 2} {3, 4} {5, 6} {7, 8} {9, 10}};

System.out.println("Before shuffle");

displayMatrix(m);

System(dot)out(dot)println("After shuffle");

shuffle(m);

displayMatrix(m);

}

public_static_void_displayMatrix_(int[][] m) {

for (int i = 0; i < m.length; i++) {

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

System.out.print(m[i][j] + " ");

}

System(dot)out(dot)println("");

}

}

public_static_void_shuffle_(int[][] m) {

int r1; // random row index

for (int i = 0; i < m.length; i++) {

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

do {

r1 = (int)(Math.random() * m.length);

} while (r1 == i);

int[] temp = m[i];

m[i] = m[r1];

m[r1] = temp;

}

}

}

}

User DawnYu
by
5.5k points