public class SwapMultidimensional { // TODO - write your code below this comment
ID: 3915112 • Letter: P
Question
public class SwapMultidimensional { // TODO - write your code below this comment. // 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. // 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(" Before swapping columns 1 and 2:"); print2D(example2); swapCols(example2, 1, 2); System.out.println("After swapping columns 1 and 2:"); print2D(example2); } }
Explanation / Answer
public class Main {
public static void swapRows(int[][] a, int one, int two)
{
// TAKING row into temporary array and swapping using that array
int[] tempRow = a[one];
a[one] = a[two];
a[two] = tempRow;
}
public static void swapCols(int[][] a, int one, int two)
{
// going through each row
for(int i=0; i<a.length; i++)
{
// swapping numbers in the given columns
int temp = a[i][one];
a[i][one] = a[i][two];
a[i][two] = temp;
}
}
// 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(" Before swapping columns 1 and 2:");
print2D(example2);
swapCols(example2, 1, 2);
System.out.println("After swapping columns 1 and 2:");
print2D(example2);
}
}
/*SAMPLE OUTPUT
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
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.