Manipulation of 2D arrays is testable on the AP Computer Science A Exam. Multiple choice and free response questions are fair game. The DeterminantFinder practice problem is intended as practice with 2D arrays and with recursion.

The DeterminantFinder practice problem requires complex manipulation of 2D arrays of integers. It also requires coding a recursive method of significant complexity. DeterminantFinder is not intended to simulate an AP style free response question and should not be used as part of a practice test or for timing.

Complete the MatrixManipulator practice problem before attempting the DeterminantFinder practice problem.

DeterminantFinder instructions

A determinant is a mathematical value calculated from a square matrix. Wikipedia has a excellent article on determinants. There are also many other resources that explain what determinants are and how to find them. Review these first to gain an understanding of the math.

The Java files below include skeleton code for each method and a JUnit 5 tester for each method. See Running JUnit 5 tests.

DeterminantFinder.java
DeterminantFinderTest.java

determinantDefined method

/**
 * Determines if the determinant is defined for matrix.
 * @return true if the determinant is defined, false otherwise
 */
public static boolean determinantDefined(int[][] matrix)

determinantDefined explanation

A determinant is defined for matrix if both of the following are true.

As with all AP CS 2D array problems, it is assumed that each row in matrix is the same length.

determinantDefined examples

int[][] mat = new int[][] {
    {3,  7},
    {1, -4}
};

System.out.println(DeterminantFinder.determinantDefined(mat)); // prints true

mat = new int[][] {
    {3,  7,  8},
    {1, -4,  6},
    {5,  6,  9}
};

System.out.println(DeterminantFinder.determinantDefined(mat)); // prints true

mat = new int[][] {
    {3,  7},
    {1, -4},
    {5,  6}
};

System.out.println(DeterminantFinder.determinantDefined(mat)); // prints false

mat = new int[][] {
    {1}
};

System.out.println(DeterminantFinder.determinantDefined(mat)); // prints false

findTwoByTwoDeterminant method

/**
 * Calculates the determinant of matrix, which must be a 2x2 matrix.
 * Precondition: matrix.length == 2 && matrix[0].length == 2 
 * @return the determinant
 */
public static int findTwoByTwoDeterminant(int[][] matrix)

findTwoByTwoDeterminant explanation

The determinant of a 2 by 2 matrix is defined as a simple algebraic expression involving the 4 values. See the Wikipedia article linked above.

findTwoByTwoDeterminant example

int[][] mat = new int[][] {
    {3,  7},
    {1, -4}
};

System.out.println(DeterminantFinder.findTwoByTwoDeterminant(mat)); // prints -19

withoutRowAndColumn method

/**
 * Calculates a matrix identical to matrix with the specific row and column removed.
 * Precondition: rowToRemove >= 0 && rowTorRemove < matrix.length &&
 *               colToRemove >= 0 && colToRemove < matrix[0].length
 * @return a matrix identical to matrix without the specified row and column.
 */
private static int[][] withoutRowAndColumn(int[][] matrix, int rowToRemove, int colToRemove)

withoutRowAndColumn examples

int[][] mat = new int[][] {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
};

int[][] result = DeterminantFinder.withoutRowAndColumn(mat, 0, 0);
System.out.println(Arrays.deepToString(result)); // prints:
// [[5, 6],
//  [8, 9]]

result = DeterminantFinder.withoutRowAndColumn(mat, 1, 1);
System.out.println(Arrays.deepToString(result)); // prints:
// [[1, 3],
//  [7, 9]]

result = DeterminantFinder.withoutRowAndColumn(mat, 0, 1);
System.out.println(Arrays.deepToString(result)); // prints:
// [[4, 6],
//  [7, 9]]

result = DeterminantFinder.withoutRowAndColumn(mat, 1, 0);
System.out.println(Arrays.deepToString(result)); // prints:
// [[2, 3]
//  [8, 9]]

findDeterminant method

/**
 * Calculates the determinant of matrix.
 * Precondition: determinantDefined(matrix)
 * @return the determinant.
 */
public static int findDeterminant(int[][] matrix)

findDeterminant explanation

The findDeterminant method finds the determinant of any matrix for which a determinant is defined.

findDeterminant examples

int[][] mat = new int[][] {
    {-2, -1,  2},
    { 2,  1,  4},
    {-3,  3, -1}
};

System.out.println(DeterminantFinder.findDeterminant(mat)); // prints 54

mat = new int[][] {
    { 1, -2,  4,  0},
    { 7,  3,  0,  3},
    {-1,  1, -4,  0},
    { 0,  3,  2,  1}
};

System.out.println(DeterminantFinder.findDeterminant(mat)); // prints -34

Solution & comments

See the DeterminantFinder solution or review it with AP CS Tutor Brandon Horn.

Comment on DeterminantFinder