Matrix Multiplication in C

Two matrices can be multiplied when the number of columns in the first matrix equals the number of rows in the second matrix. If matrix A is r1 × c1 and matrix B is r2 × c2, multiplication requires c1 == r2, and the result has size r1 × c2.

2 × 3 by 3 × 2 Matrix Multiplication Top ↑

#include <stdio.h>

int main(void) {
    int first[2][3] = {
        {1, 2, 3},
        {4, 5, 6}
    };

    int second[3][2] = {
        {7, 8},
        {9, 10},
        {11, 12}
    };

    int result[2][2] = {0};

    for (int row = 0; row < 2; row++) {
        for (int column = 0; column < 2; column++) {
            for (int k = 0; k < 3; k++) {
                result[row][column] +=
                    first[row][k] * second[k][column];
            }
        }
    }

    for (int row = 0; row < 2; row++) {
        for (int column = 0; column < 2; column++) {
            printf("%4d", result[row][column]);
        }
        putchar('\n');
    }

    return 0;
}

Output

58  64
139 154

How One Result Cell Is Calculated Top ↑

result[row][column] +=
    first[row][k] * second[k][column];

Each result cell is the sum of products from one row of the first matrix and one column of the second matrix.

User-Entered Matrix Multiplication Top ↑

#include <stdio.h>

#define MAX 10

int main(void) {
    int r1, c1, r2, c2;
    int first[MAX][MAX];
    int second[MAX][MAX];
    int result[MAX][MAX] = {0};

    printf("Rows and columns of first matrix (1..10): ");
    if (scanf("%d %d", &r1, &c1) != 2 ||
        r1 < 1 || r1 > MAX ||
        c1 < 1 || c1 > MAX) {
        printf("Invalid first-matrix size.\n");
        return 1;
    }

    printf("Rows and columns of second matrix (1..10): ");
    if (scanf("%d %d", &r2, &c2) != 2 ||
        r2 < 1 || r2 > MAX ||
        c2 < 1 || c2 > MAX) {
        printf("Invalid second-matrix size.\n");
        return 1;
    }

    if (c1 != r2) {
        printf("Cannot multiply: c1 must equal r2.\n");
        return 1;
    }

    printf("Enter first matrix:\n");
    for (int row = 0; row < r1; row++) {
        for (int column = 0; column < c1; column++) {
            if (scanf("%d", &first[row][column]) != 1) {
                printf("Invalid input.\n");
                return 1;
            }
        }
    }

    printf("Enter second matrix:\n");
    for (int row = 0; row < r2; row++) {
        for (int column = 0; column < c2; column++) {
            if (scanf("%d", &second[row][column]) != 1) {
                printf("Invalid input.\n");
                return 1;
            }
        }
    }

    for (int row = 0; row < r1; row++) {
        for (int column = 0; column < c2; column++) {
            for (int k = 0; k < c1; k++) {
                result[row][column] +=
                    first[row][k] * second[k][column];
            }
        }
    }

    printf("Product matrix:\n");
    for (int row = 0; row < r1; row++) {
        for (int column = 0; column < c2; column++) {
            printf("%d ", result[row][column]);
        }
        putchar('\n');
    }

    return 0;
}

Common Matrix-Multiplication Mistakes Top ↑

  • Trying to multiply incompatible dimensions.
  • Forgetting to initialize each result cell before accumulating products.
  • Mixing the row, column and shared-dimension indexes.
  • Reading matrix dimensions larger than the declared arrays.

See also matrix addition and subtraction and two-dimensional arrays.





plus2net.com






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer