Sunday, 28 August 2016

Chapter 8 Exercise 18, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

*8.18 (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)

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



public class ProgrammingEx8_18 {
 
 public static void main(String[] args) {
  int[][] m = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 }, { 9, 10 } };
  shuffle(m);
 
  System.out.print("{");
  for (int i = 0; i < m.length; i++) {
   System.out.print("{");
   for (int j = 0; j < m[0].length; j++) {
    System.out.print(m[i][j]);
    if (j != m[0].length - 1) {
     System.out.print(",");
    } else if (i == m.length - 1) {
     System.out.print("}");
    } else {
     System.out.print("},");
    }
 
   }
  }
  System.out.print("};");
 }
 
 public static void shuffle(int[][] m) {
  int row = m.length;
  int col = m[0].length;
  int total = row * col;
 
  // Go through all the positions in metrix
  // Select member as random and put it in current position
  for (int i = 0; i < total; i++) {
   int sel = intRandom(i, total - 1);
   // selected member as random
   int srow = sel / row;
   int scol = sel % col;
   // calculate row and column of the current member
   int crow = i / row;
   int ccol = i % col;
 
   // swap current member with selected member
   int temp = m[srow][scol];
   m[srow][scol] = m[crow][ccol];
   m[crow][ccol] = temp;
 
  }
 
 }
 
 public static int intRandom(int lowerBound, int upperBound) {
  return (int) (lowerBound + Math.random()
    * (upperBound - lowerBound + 1));
 }
 
}

No comments :

Post a Comment