[an error occurred while processing this directive]
CIS 110

Recitation 2 Exercises (17/18 October 2012)

Before each recitation we will post a short set of written exercises. These are intended to help you prepare for recitation. You must complete them to the best of your ability, and bring them to recitation. Your TAs will note whether you have completed them along with your attendance. These exercises will not be graded for correctness, but you must attempt to answer them and bring your answers to recitation. A portion of your grade depends on your attempting these exercises each week and attending recitation.

The questions below relate to the output of the following class, which includes three different functions for transposing a 2-D array (swapping its rows and columns). Each question provides a different main method. You can cut and paste the code into Dr. Java, but if you have time it is worth trying to think briefly about what will happen in each case before testing it.

public class Transpose {
  public static void transpose1(int[][] arr) {
    for (int row = 0; row < arr.length; row++) {
      for (int col = 0; col < row; col++) {
        // swap the elements at a[row][col] and a[col][row]
        int tmp = arr[row][col];
        arr[row][col] = arr[col][row];
        arr[col][row] = tmp;
      }
    }
  }

  public static void transpose2(int[][] arr) {
    int rows = arr.length;
    int cols = arr[0].length;

    int[][] arr2 = new int[rows][cols];
    for (int row = 0; row < rows; row++)
      for (int col = 0; col < cols; col++)
        arr2[row][col] = arr[row][col];

    transpose1(arr2);
  }

  public static int[][] transpose3(int[][] arr) {
    int rows = arr.length;
    int cols = arr[0].length;

    int[][] out = new int[cols][rows];
    for (int row = 0; row < rows; row++)
      for (int col = 0; col < cols; col++)
        out[col][row] = arr[row][col];

    return out;
  }

  // print out the array a
  public static void printArray(int[][] a) {
    for (int row = 0; row < a.length; row++) {
      for (int col = 0; col < a[row].length; col++)
        System.out.print(a[row][col] + " ");
      System.out.println();
    }
  }
}
Question 1 What will the following main function print if you insert it into the Transpose class above and run it?

  public static void main(String[] args) {
    // a is a 3 x 3 array
    int[][] a = { { 1, 2, 3 },
                  { 4, 5, 6 },
                  { 7, 8, 9 } };

    transpose1(a);
    printArray(a);
  }





Question 2 What will the following main function print if you insert it into the Transpose class above and run it?

  public static void main(String[] args) {
    // a is a 3 x 3 array
    int[][] a = { { 1, 2, 3 },
                  { 4, 5, 6 },
                  { 7, 8, 9 } };

    transpose2(a);
    printArray(a);
  }





Question 3 What will the following main function print if you insert it into the Transpose class above and run it?

  public static void main(String[] args) {
    // a is a 2 x 3 array
    int[][] a = { { 1, 2, 3 },
                  { 4, 5, 6 } };

    transpose1(a);
    printArray(a);
  }





Question 4 What will the following main function print if you insert it into the Transpose class above and run it?

  public static void main(String[] args) {
    // a is a 3 x 2 array
    int[][] a = { { 1, 2 },
                  { 3, 4 },
                  { 5, 6 } };

    transpose1(a);
    printArray(a);
  }





Question 5 What will the following main function print if you insert it into the Transpose class above and run it?

  public static void main(String[] args) {
    // a is a 3 x 2 array
    int[][] a = { { 1, 2 },
                  { 3, 4 },
                  { 5, 6 } };

    int[][] b = transpose3(a);
    printArray(b);
  }