Perform Matrix Division in C

Beginner
⏱️ 11 min read
📚 Updated: May 2026
🎯 2 Code Examples
Tables & division

What you’ll learn

  • In plain English: how to divide two same-sized tables by dividing matching cells (like stacking two spreadsheets and dividing top ÷ bottom in each cell).
  • Why programs here use float numbers, and why divide-by-zero must be handled.
  • Two short C examples: a straight 2×2 demo and a version that refuses to divide when the bottom cell is zero.

Overview

Picture two grids with the same number of rows and columns. For each box, you divide the number on top by the number directly below it. That is all this lesson implements in C. We call it element-wise division.

Two programs

A tiny 2×2 example with decimals, plus a safe version that checks for zeros.

Live preview

See the same numbers as example 1 computed in your browser.

Honest wording

We separate this simple idea from harder “inverse matrix” math so beginners do not get lost.

Before you start

You only need basic arithmetic (division), plus the idea of a loop that visits each row and column.

  • You can read a tiny C program with #include <stdio.h> and printf.
  • You know that dividing by zero is not allowed.

In plain English

A matrix here is just a rectangle of numbers. If matrix A and matrix B have the same shape, the result in row i, column j is:

answer[i][j] = A[i][j] ÷ B[i][j]

Example: top-left is 4 ÷ 2 = 2, next cell is 8 ÷ 4 = 2, and so on. Each spot is independent: you never mix numbers from different boxes.

Later, you may hear “inverse matrices”

In advanced courses, “dividing by a matrix” can mean something deeper (multiply by an inverse). This tutorial does not do that. It only divides number-by-number in matching cells. Both ideas are real; they are not the same program.

Pocket calculator picture

Treat each pair like pressing “A cell ÷ B cell” on a calculator.

4 ÷ 2 = 2
Where
Top-left of the 2×2 example
8 ÷ 4 = 2
Where
Top-right of the same example

Every other cell in the sample works the same way. That is why the final table is all 2.00 in this particular exercise.

Live preview

These are the same starting numbers as code example 1. Press the button to see A, B, and A ÷ B (cell by cell).

No typing needed—good for a quick check before you compile.

Live result
Press “Show 2×2 division”.

Algorithm (simple steps)

Goal: fill a new table so each cell equals the top number divided by the bottom number in that cell.

Line up the grids

Both tables must have the same height and width. If they do not, this simple method does not apply.

Walk each row and column

Use one loop for rows and one loop for columns (nested loops). Visit every cell once.

Divide carefully

Before dividing, make sure the bottom value is not zero. Then store a[i][j] / b[i][j] in the answer table.

📜 Pseudocode

Pseudocode
function divide_elementwise(A, B, Result, rows, cols):
    for i from 0 to rows - 1:
        for j from 0 to cols - 1:
            if B[i][j] is zero:
                stop with an error (cannot divide)
            Result[i][j] ← A[i][j] / B[i][j]
1

2×2 element-wise division (main example)

This matches the classic walkthrough: float values, two nested loops, and a print helper. The sample numbers are chosen so every bottom cell is nonzero, so division is always safe.

c
#include <stdio.h>

#define ROWS 2
#define COLS 2

void print_matrix(float m[ROWS][COLS]) {
    for (int i = 0; i < ROWS; ++i) {
        for (int j = 0; j < COLS; ++j) {
            printf("%0.2f\t", m[i][j]);
        }
        printf("\n");
    }
}

void divide_matrices(float a[ROWS][COLS], float b[ROWS][COLS], float out[ROWS][COLS]) {
    for (int i = 0; i < ROWS; ++i) {
        for (int j = 0; j < COLS; ++j) {
            out[i][j] = a[i][j] / b[i][j];
        }
    }
}

int main(void) {
    float matrix_a[ROWS][COLS] = {
        {4.0f, 8.0f},
        {2.0f, 6.0f}
    };
    float matrix_b[ROWS][COLS] = {
        {2.0f, 4.0f},
        {1.0f, 3.0f}
    };
    float result[ROWS][COLS];

    divide_matrices(matrix_a, matrix_b, result);

    printf("Result of matrix division:\n");
    print_matrix(result);

    return 0;
}

What each part does

divide_matrices is the heart: one division per cell. print_matrix only prints; it does not change the math.

out[i][j] = a[i][j] / b[i][j];

Same address, two tables. i picks the row, j picks the column. Always the same (i, j) on both sides.

printf("%0.2f\t", ...);

Two decimal places so the output looks tidy. The tab \t adds space between columns.

2

Same idea, but stop if a divisor is zero

Real programs should not silently divide by zero. This version checks B first. If it finds a zero, it prints a short message and stops.

c
#include <stdio.h>

#define ROWS 2
#define COLS 2

int b_has_zero(float b[ROWS][COLS]) {
    for (int i = 0; i < ROWS; ++i) {
        for (int j = 0; j < COLS; ++j) {
            if (b[i][j] == 0.0f) {
                return 1;
            }
        }
    }
    return 0;
}

void divide_matrices(float a[ROWS][COLS], float b[ROWS][COLS], float out[ROWS][COLS]) {
    for (int i = 0; i < ROWS; ++i) {
        for (int j = 0; j < COLS; ++j) {
            out[i][j] = a[i][j] / b[i][j];
        }
    }
}

void print_matrix(const char *title, float m[ROWS][COLS]) {
    printf("%s\n", title);
    for (int i = 0; i < ROWS; ++i) {
        for (int j = 0; j < COLS; ++j) {
            printf("%0.2f\t", m[i][j]);
        }
        printf("\n");
    }
}

int main(void) {
    float a[ROWS][COLS] = {{4.0f, 8.0f}, {2.0f, 6.0f}};
    float b[ROWS][COLS] = {{2.0f, 4.0f}, {1.0f, 3.0f}};
    float r[ROWS][COLS];

    if (b_has_zero(b)) {
        printf("Cannot divide: matrix B contains a zero.\n");
        return 1;
    }

    divide_matrices(a, b, r);

    print_matrix("A", a);
    printf("\n");
    print_matrix("B", b);
    printf("\n");
    print_matrix("A / B (cell by cell)", r);

    return 0;
}

Why this matters

Comparing float with == 0 is not perfect in serious floating-point work, but it is easy to read for a first course. For money or science-grade code, people use tolerances or separate validation rules.

Going further (optional)

Bigger tables. Change ROWS and COLS, or read sizes from the user, as long as both matrices still match.

Need true matrix division in the math-club sense? That usually means learning matrix inverses and matrix multiplication. Treat that as a separate project; this page stays with the easy cell-by-cell idea.

❓ FAQ

Yes. Think of a matrix as a table of numbers. This program only does normal division in each box: top number ÷ bottom number in the same box. No special math course is required for that part.
It builds a new table. In every row and column, it takes the value from matrix A, divides it by the value from matrix B in the same position, and stores the answer. That is called element-wise (or entry-wise) division.
Often, no. In higher math, dividing by a matrix can mean multiplying by an inverse matrix. This tutorial does the simpler thing: divide matching cells. We say that clearly so you are not surprised later.
Division often gives decimals (for example 5 ÷ 2 = 2.5). The type float can store those decimal answers. int would drop the fraction or surprise you with rounding.
You cannot divide by zero. The first example only uses safe numbers. The second program checks before dividing and stops with a clear error message if it finds a zero in the bottom matrix.
It visits every cell once, so the time grows in proportion to the number of cells (rows times columns). Memory is small: you mostly store the two input tables and the result.

🔄 Input and output

These samples put numbers directly in the code. To try your own values, change the tables inside main (or later learn scanf to type them at runtime). Always keep A and B the same size, and keep every B cell nonzero unless you add error handling.

Watch out for

Three practical reminders while you are learning:

Zero

Division by zero

Never divide if the bottom cell is zero. Example 2 shows one simple guard.

Shape

Mismatched tables

This method needs the same number of rows and columns in both matrices.

Words

Name confusion

Say “element-wise division” if a teacher asks, so they know you mean cell-by-cell, not inverse matrices.

⏱️ Time and space (big picture)

TaskTimeExtra memory
Divide two m × n tables this wayProportional to m · n (each cell once)Small: mostly the output table

You do not need to memorize symbols here. The idea is: bigger grids take longer because there are more cells.

Summary

  • Idea: divide each top cell by the bottom cell in the same position. Same shape for both tables.
  • Code: two nested loops and float division; guard against zero if inputs vary.
  • Not the same as: multiplying by an inverse matrix (advanced topic).
Did you know?

This page uses cell-by-cell division: each number is only divided by the number in the same row and column. That is a simple idea. University math also talks about A × B−1 for “dividing” matrices—that is a different, harder topic.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful