Lab 12 Pre-Lab Assignment      Name ____________________________________________

  1. int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    
    Given the above definition for the two dimensional array of ints called matrix, 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. It is also possible to have 2D arrrays that are not rectangular. Given the following definition of a ragged array. Write code that would print the array to the command line (assuming that the first dimension specifies the row of an element and the second specifies the column).
    int[][] raggedMatrix = {{1, 2, 3},
                           {4, 5},
                           {6}};
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

  3. Complete the following methods:
    1. /*
       * Returns true if the specified matrix is square, false otherwise.
       * (In a square matrix every row has as many elements in it as there are rows)
       */
      public boolean isSquare(int[][] matrix) {
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      }
      
    2. /*
       * Returns the sum of all of the elements in the specified matrix's main diagonal.
       * If the matrix is not square it returns -1.
       * (The main diagonal consists of the elements for which the index of the row
       * and the column are the same.)
       */
      public int getDiagonalSum(int[][] matrix) {
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      }