Multi-Dimensional Array Exercises

  1. Write a method isRow which accepts a two dimensional array of doubles and an int row number as parameters and returns true if that row is present in the array and false otherwise. Return false if the array is null or empty.
    HINT: Consider the following tests:
          double[][] data = {{}, {1}, {2, 3}};
          System.out.println(isRow(data, -1));
          System.out.println(isRow(data, 0));
          System.out.println(isRow(data, 1));
          System.out.println(isRow(data, 2));
          System.out.println(isRow(data, 3));
          System.out.println(isRow(null, 1));
          data[1] = null;
          System.out.println(isRow(data, 1));
    
  2. Write a method isCol which accepts a two dimensional array of doubles and an int column number as parameters and returns true if at least one of the rows has that column value and false otherwise. Return false if the array is null or empty.
    HINT: Consider the following tests:
          double[][] data = {{1}, {2, 3}};
          System.out.println(isCol(data, -1));
          System.out.println(isCol(data, 0));
          System.out.println(isCol(data, 1));
          System.out.println(isCol(data, 2));
          System.out.println(isCol(null, 1));
          data[0] = null;
          System.out.println(isCol(data, 0));
    
  3. Write a method sumColumn which accepts a two dimensional array of doubles and an int column number as parameters and returns the sum of the elements in the given column. If the column number is not a valid column for that array or the array is null or empty, throw an ArrayIndexOutOfBoundsException passing the column number as a parameter to the constructor. HINT: See # 2
  4. Write a method getHighestInRow which accepts a two dimensional array of doubles and an int row number as parameters and returns the highest value of the elements in the given row. If the row number is not a valid row for that array or the row or array are null or empty, throw an ArrayIndexOutOfBoundsException passing the row number as a parameter to the constructor. HINT: See # 2
  5. Write a method getTotal which accepts a two dimensional array of doubles as a parameter and returns the total of all the elements in the matrix. If the array is null or empty, return Double.MIN_VALUE.

These exercises were originally developed by Nancy Harris.