C Program to Find Determinant of a Matrix

C Program to Find Determinant of a Matrix

A small school table has two rows and two columns. The 2×2 determinant operation should preserve what each row and column means, rather than treating the table as four unrelated values.

The determinant of a two-by-two matrix is its main-diagonal product minus its other-diagonal product.

Before this exercise, review array indexes and bounds and nested loops. The worked program below applies those ideas to this specific problem.

  1. Explain

    Explain why 2×2 determinant uses this sequence of operations.

  2. Trace

    Trace the sample values and predict the printed result.

  3. Apply

    Make the second row twice the first row and calculate the determinant.

2×2 Determinant: follow the data

  1. Given data

    A small school table has two rows and two columns. The 2×2 determinant operation should preserve what each row and column means, rather than treating the table as four unrelated values.

  2. Rule

    The determinant of a two-by-two matrix is its main-diagonal product minus its other-diagonal product.

  3. Observed result

    Determinant: -2

A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Understand the operation

For [[a,b],[c,d]], calculate ad-bc. A zero determinant identifies a singular two-by-two transformation, while a negative value can represent orientation reversal.

The example fixes both dimensions at two so every index can be checked directly. A general matrix interface must carry dimensions, require compatible shapes for its operation and validate storage for each referenced element.

Read the complete C program

The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.

C17 · complete program

#include <stdio.h>
int main(void) {
  int matrix[2][2] = {{1, 2}, {3, 4}};
  int determinant = matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
  printf("Determinant: %d\n", determinant);
  return 0;
}

Expected output

Determinant: -2
Run in C compiler →

Follow the values through the code

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  int matrix[2][2] = {{1, 2}, {3, 4}};
  int determinant = matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
  printf("Determinant: %d\n", determinant);
  return 0;
}

The products are 1×4=4 and 2×3=6, so their difference is -2.

Read the resulting rows in order; moving a value to another row or column is part of the operation’s meaning, not merely an output-format choice.

Follow the worked calculation

  1. Start with the sample

    The products are 1×4=4 and 2×3=6, so their difference is -2.

  2. Apply the rule

    Read the resulting rows in order; moving a value to another row or column is part of the operation’s meaning, not merely an output-format choice.

  3. Check the result

    Determinant: -2

    Determinant: -2
A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Try a small variation

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  int matrix[2][2] = {{1, 2}, {3, 4}};
  int determinant = matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
  printf("Determinant: %d\n", determinant);
  return 0;
}

Make the second row twice the first row and calculate the determinant.

Worked answer

The complete worked answer uses the same valid two-by-two input. Trace its index rule independently; compare each printed result with the source element or accumulation that produced it.

C17 · complete program

#include <stdio.h>
int main(void) {
  int matrix[2][2] = {{1, 2}, {3, 4}};
  matrix[1][0] = 2; matrix[1][1] = 4;
  int determinant = matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
  printf("Determinant: %d\n", determinant);
  return 0;
}

Expected output

Determinant: 0
Run in C compiler →

Recognize the important boundaries

The two-product formula is specific to size two. It is not a general determinant algorithm for a larger table.

Larger or untrusted dimensions require checked storage-size calculations. Integer products and accumulated sums also need a representable-range contract.

Check your understanding

Choose the answer, then compare the reasoning for every option.

What must be known before indexing a general two-dimensional table?
Choose one answer. You can try again.

Answer and explanation

Its dimensions and compatible storage layout.

Correct. A row/column access is valid only within the corresponding bounds.

Use the idea independently

The determinant of a two-by-two matrix is its main-diagonal product minus its other-diagonal product.

Continue with array boundaries.

Continue with function bounds contracts.

Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: C Program to Find Matrix Normal and Trace →