Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,851 questions

51,772 answers

573 users

How to multiply two matrices (matrix) in C

2 Answers

0 votes
#include <stdio.h>

#define COLS1 3
#define COLS2 2

void multiplyMatrices(const int matrix1[][COLS1], const int matrix2[][COLS2], 
                      const int rows1, int result[][COLS2]) {
    for (int i = 0; i < rows1; i++) {
        for (int j = 0; j < COLS2; j++) {
            result[i][j] = 0;
            for (int k = 0; k < COLS1; k++) {
                result[i][j] += matrix1[i][k] * matrix2[k][j];
                printf("result[%d][%d] += matrix1[%d][%d] * matrix2[%d][%d]\n", i, j, i, k, k, j);
            }
            printf("\n");
        }
    }
}

int main() {
    int matrix1[][COLS1] = { {4, 2, 4},
                             {8, 3, 1} };
    int matrix2[][COLS2] = { {3, 5},
                             {2, 8},
                             {7, 9} };
    int result[][COLS2] = { {0, 0},
                            {0, 0} };
                            
    int rows1 = sizeof(matrix1) / sizeof(matrix1[0]);

    multiplyMatrices(matrix1, matrix2, rows1, result);

    printf("Result matrix:\n");
    for (int i = 0; i < rows1; i++) {
        for (int j = 0; j < COLS2; j++)
            printf("%3d", result[i][j]);
        printf("\n");
    }

    return 0;
}



/*
run:

result[0][0] += matrix1[0][0] * matrix2[0][0]
result[0][0] += matrix1[0][1] * matrix2[1][0]
result[0][0] += matrix1[0][2] * matrix2[2][0]

result[0][1] += matrix1[0][0] * matrix2[0][1]
result[0][1] += matrix1[0][1] * matrix2[1][1]
result[0][1] += matrix1[0][2] * matrix2[2][1]

result[1][0] += matrix1[1][0] * matrix2[0][0]
result[1][0] += matrix1[1][1] * matrix2[1][0]
result[1][0] += matrix1[1][2] * matrix2[2][0]

result[1][1] += matrix1[1][0] * matrix2[0][1]
result[1][1] += matrix1[1][1] * matrix2[1][1]
result[1][1] += matrix1[1][2] * matrix2[2][1]

Result matrix:
 44 72
 37 73

*/

 



answered Jul 7, 2020 by avibootz
edited Jul 10, 2025 by avibootz
0 votes
#include <stdio.h>

#define COLS1 3
#define COLS2 2

void multiplyMatrices2x3By3x2(const int matrix1[][COLS1],
                              const int matrix2[][COLS2],
                              const int rows1,
                              int result[][COLS2]) {
    for (int i = 0; i < rows1; i++) {
        for (int j = 0; j < COLS2; j++) {
            result[i][j] = matrix1[i][0] * matrix2[0][j] +
                           matrix1[i][1] * matrix2[1][j] +
                           matrix1[i][2] * matrix2[2][j];
        }
    }
}

int main() {
    int matrix1[][COLS1] = { {1, 2, 3},
                             {4, 5, 6} };
    int matrix2[][COLS2] = { {10, 11},
                             {20, 21},
                             {30, 31} };
    int result[][COLS2] = { {0, 0},
                            {0, 0} };
                                 
    int rows1 = sizeof(matrix1) / sizeof(matrix1[0]);

    multiplyMatrices2x3By3x2(matrix1, matrix2, rows1, result);

    printf("Final result:\n");
    for (int i = 0; i < rows1; i++) {
        for (int j = 0; j < COLS2; j++) {
            printf("%3d ", result[i][j]);
        }
        printf("\n");
    }

    return 0;
}



/*
run:

Final result:
140 146 
320 335 

*/

 



answered Jul 10, 2025 by avibootz
...