In: Computer Science
Download the SwapMultidimensional.java file, and open it in jGrasp (or a text editor of your choice). This program contains two methods which you will need to write:
main contains some code which will call swapRows and swapCols, for the purpose of informal testing. With this in mind, example output of the program is below:
Before swapping rows 0 and 2: 0 1 2 3 4 5 6 7 8 After swapping rows 0 and 2: 6 7 8 3 4 5 0 1 2 Before swapping columns 1 and 2: 9 8 7 6 5 4 3 2 1 After swapping columns 1 and 2: 9 7 8 6 4 5 3 1 2
-----------------------------------------------------------------------
public class SwapMultidimensional { // You must write TWO methods: // // 1.) A method named "swapRows" that swaps the // contents of two rows in a given two-dimensional // array. This method MUST NOT use loops. As a hint, // since a two-dimensional array is just an array of // arrays where each inner array is a row, this means // that you can swap entire rows just by swapping two // elements of the outer array. // // 2.) A method named "swapCols" that swaps the contents // of two columns in a given two-dimensional array. // This method MUST use a loop. // TODO - write your code below this comment. // DO NOT MODIFY print2D! public static void print2D(int[][] array) { for (int row = 0; row < array.length; row++) { for (int col = 0; col < array[row].length - 1; col++) { System.out.print(array[row][col] + " "); } System.out.println(array[row][array[row].length - 1]); } } // DO NOT MODIFY main! public static void main(String[] args) { int[][] example1 = new int[][]{ new int[]{0, 1, 2}, new int[]{3, 4, 5}, new int[]{6, 7, 8} }; int[][] example2 = new int[][]{ new int[]{9, 8, 7}, new int[]{6, 5, 4}, new int[]{3, 2, 1} }; System.out.println("Before swapping rows 0 and 2:"); print2D(example1); swapRows(example1, 0, 2); System.out.println("After swapping rows 0 and 2:"); print2D(example1); System.out.println("\nBefore swapping columns 1 and 2:"); print2D(example2); swapCols(example2, 1, 2); System.out.println("After swapping columns 1 and 2:"); print2D(example2); } }
If you have any doubts, please give me comment...
public class SwapMultidimensional {
// You must write TWO methods:
//
// 1.) A method named "swapRows" that swaps the
// contents of two rows in a given two-dimensional
// array. This method MUST NOT use loops. As a hint,
// since a two-dimensional array is just an array of
// arrays where each inner array is a row, this means
// that you can swap entire rows just by swapping two
// elements of the outer array.
//
public static void swapRows(int[][] arr, int r1, int r2){
int temp[] = arr[r1];
arr[r1] = arr[r2];
arr[r2] = temp;
}
// 2.) A method named "swapCols" that swaps the contents
// of two columns in a given two-dimensional array.
// This method MUST use a loop.
// TODO - write your code below this comment.
public static void swapCols(int[][] arr, int c1, int c2){
int rLen = arr.length;
int temp[] = new int[rLen];
for(int i=0; i<rLen; i++)
temp[i] = arr[i][c1];
for(int i=0; i<rLen; i++)
arr[i][c1] = arr[i][c2];
for(int i=0; i<rLen; i++)
arr[i][c2] = temp[i];
}
// DO NOT MODIFY print2D!
public static void print2D(int[][] array) {
for (int row = 0; row < array.length; row++) {
for (int col = 0; col < array[row].length - 1; col++) {
System.out.print(array[row][col] + " ");
}
System.out.println(array[row][array[row].length - 1]);
}
}
// DO NOT MODIFY main!
public static void main(String[] args) {
int[][] example1 = new int[][]{ new int[]{0, 1, 2},
new int[]{3, 4, 5},
new int[]{6, 7, 8} };
int[][] example2 = new int[][]{ new int[]{9, 8, 7},
new int[]{6, 5, 4},
new int[]{3, 2, 1} };
System.out.println("Before swapping rows 0 and 2:");
print2D(example1);
swapRows(example1, 0, 2);
System.out.println("After swapping rows 0 and 2:");
print2D(example1);
System.out.println("\nBefore swapping columns 1 and 2:");
print2D(example2);
swapCols(example2, 1, 2);
System.out.println("After swapping columns 1 and 2:");
print2D(example2);
}
}