Skip to content
C project #14Intermediate

Matrix Toolkit

Dynamically allocated matrices with addition, multiplication, transpose, determinant and inverse via Gauss–Jordan elimination.

Estimated time
~6 hours to build
Files
7 files · 415 lines
Technologies
C11, <math.h>, Dynamic memory

Introduction

Linear algebra powers graphics, machine learning and engineering simulations. This toolkit implements the core matrix operations in C with heap-allocated, contiguous storage — the same layout numeric libraries use.

Problem statement

Read matrices of user-chosen size and compute A + B, A × B, the transpose, the determinant (with partial pivoting) and the inverse (Gauss–Jordan), detecting dimension mismatches and singular matrices, and verifying the inverse by multiplication.

Objectives

  • Store a 2D matrix in a single malloc'd block
  • Implement elimination algorithms with partial pivoting
  • Return results by value with an error convention
  • Manage memory for many temporary matrices without leaks

Features

  • Any size up to 20 × 20
  • Dimension checks for addition and multiplication
  • Determinant with row-swap sign tracking
  • Inverse with singularity detection and automatic verification

Concepts demonstrated

  • Row-major storage
  • Gaussian elimination
  • Numerical stability
  • Ownership of returned structs

System architecture

Matrix holds rows, cols and a double *data where element (i, j) is data[i*cols + j]. Every operation returns a new Matrix; a data == NULL result signals an error. mat_inverse augments A with the identity and reduces it to [I | A⁻¹]. The multiply loop uses i-k-j order for cache efficiency.

Folder structure

TextProject tree
matrix-toolkit/
├── README.md
├── Makefile
├── src/main.c
├── src/matrix.c
├── src/matrix.h
├── tests/expected-output.txt
└── tests/sample-input.txt

Complete source code

Makefile

20 lines
MakefileMakefile
# Generated for KENHOLT Skills. Usage: make | make test | make clean
CC = gcc
CFLAGS = -std=c11 -Wall -Wextra -O2
SRC := $(wildcard src/*.c)
HDR := $(wildcard src/*.h)
ARGS := $(shell cat tests/args.txt 2>/dev/null)
INPUT := $(wildcard tests/sample-input.txt)
app: $(SRC) $(HDR)
$(CC) $(CFLAGS) -Isrc $(SRC) -o app -lm
test: app
./app $(ARGS) $(if $(INPUT),< $(INPUT),) > actual.txt
diff actual.txt tests/expected-output.txt && echo "Test passed: output matches tests/expected-output.txt"
clean:
rm -f app app.exe actual.txt
.PHONY: test clean

What each file does

src/matrix.h
The Matrix type and the API.
src/matrix.c
Allocation, element access, add/multiply/transpose, determinant and Gauss–Jordan inverse.
src/main.c
Reading matrices and the operations menu; frees every result.
Makefile
Build automation: make builds ./app, make test runs the sample session and compares it with the expected output, make clean removes build files.

How to run it

Build

Terminal
gcc -std=c11 -Wall -Wextra -Isrc src/*.c -o app -lm
# or simply: make

Run

Terminal
./app # Linux / macOS
.\app.exe # Windows

Test against the expected output

Terminal
./app < tests/sample-input.txt > actual.txt
diff actual.txt tests/expected-output.txt # no output means they match
# Windows PowerShell:
# Get-Content tests\sample-input.txt | .\app.exe > actual.txt
# Compare-Object (Get-Content actual.txt) (Get-Content tests\expected-output.txt)

Expected output

Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.

Texttests/expected-output.txt
1.Add 2.Multiply 3.Transpose 4.Determinant 5.Inverse 0.Exit
Choice:
Rows and columns of A:
Enter 4 values row by row:
Rows and columns of B:
Enter 4 values row by row:
A + B (2x2):
6.0000 8.0000
10.0000 12.0000
1.Add 2.Multiply 3.Transpose 4.Determinant 5.Inverse 0.Exit
Choice:
Rows and columns of A:
Enter 6 values row by row:
Rows and columns of B:
Enter 6 values row by row:
A x B (2x2):
58.0000 64.0000
139.0000 154.0000
1.Add 2.Multiply 3.Transpose 4.Determinant 5.Inverse 0.Exit
Choice:
Rows and columns of A:
Enter 9 values row by row:
det(A) = 49.0000
1.Add 2.Multiply 3.Transpose 4.Determinant 5.Inverse 0.Exit
Choice:
Rows and columns of A:
Enter 4 values row by row:
Inverse of A (2x2):
0.6000 -0.7000
-0.2000 0.4000
Check: A x inverse(A) (2x2):
1.0000 0.0000
0.0000 1.0000
1.Add 2.Multiply 3.Transpose 4.Determinant 5.Inverse 0.Exit
Choice:
Rows and columns of A:
Enter 4 values row by row:
Inverse of A: not defined for these dimensions (or the matrix is singular).
1.Add 2.Multiply 3.Transpose 4.Determinant 5.Inverse 0.Exit
Choice:
Bye.
Input
1
2 2
1 2 3 4
2 2
5 6 7 8
2
2 3
1 2 3 4 5 6
3 2
7 8 9 10 11 12
4
3 3
2 -3 1 2 0 -1 1 4 5
5
2 2
4 7 2 6
5
2 2
1 2 2 4
0

Testing

  • Diff with tests/expected-output.txt.
  • The determinant of [[2,−3,1],[2,0,−1],[1,4,5]] must be 49.
  • Inverting a singular matrix (e.g. [[1,2],[2,4]]) must report it as singular.

Common errors

Inverse contains nan

Without pivoting, a zero on the diagonal causes division by zero. Always pivot on the largest absolute value.

Values like -0.0000 appear

Floating-point results near zero can be negative; mat_print prints values below 5e-5 as 0.

Possible improvements

  • Solve linear systems Ax = b
  • LU decomposition
  • Read matrices from files

Additional challenges

  1. 1Compute the rank of a matrix.
  2. 2Implement Strassen's multiplication and compare its speed for 256 × 256.
  3. 3Add an eigenvalue estimate using the power method.

Built and tested it yourself? Mark it to update your dashboard.