Lab 12 Pre-Lab Assignment      Name ____________________________________________

  1. The following instantiates a two-dimensional array, named matrix, using an initialization list. A 2-dimensional array is an array of rows, each of which is a 1-dimensional array. So, this sets the first row (index 0) to be elements 1 2 3, the second row (index 1) to be 4 5 6, and so on.

         int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    
    
    Given the above definition, what would each of the following print to the command line if run.
    1. System.out.println("matrix[1][1]: " + matrix[1][1]);
      
      
      
      
      
      
      
    2. System.out.println("matrix[0][2]: " + matrix[0][2]);
      
      
      
      
      
      
      
    3. for(int i = 0; i < matrix.length; i++) {
      	System.out.print(matrix[i][0] + " ");
      }
      
      
      
      
      
      
      
      

  2. Suppose matrix has been declared and instantiated as a two-dimensional array of ints. Suppose the variables numRows and numCols contain the number of rows and the number of columns, respectively, in the matrix.
    1. Write a segment of code to find and print the maximum element in each row.
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    2. Write a segment of code to print the elements on the main diagonal of the matrix assuming the matrix is square (the number of rows equals the number of columns). The main diagonal consists of the elements for which the index of the row and the index of the column are the same.
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    3. Write a segment of code that prints the elements of the other diagonal (this one goes from the bottom-left corner to the upper-right corner). You need to think about the relationship between the row index and the column index here! You may assume the matrix is square.