#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
*/