본문 바로가기
C, C++

[C] 4x4 행렬 곱셈

by mokhwasomssi 2021. 8. 19.
#include <stdio.h>


void multiply_matrix_4x4(int first[][4], int second[][4], int result[][4])
{
    for (int k = 0; k < 4; k++)
    {
        for (int j = 0; j < 4; j++)
        {
            for (int i = 0; i < 4; i++)
            {
                result[k][j] += first[k][i] * second[i][j];
            }
        }
    }
}


int main()
{
    int a[4][4] = { {1, 3, 5, 7}, {4, 7, 1, 2}, {3, 4, 5, 6}, {4, 7, 5, 7} };
    int b[4][4] = { {2, 4, 6, 8}, {1, 3, 5, 7}, {1, 2, 2, 9}, {1, 9, 9 ,8} };
    int result[4][4] = { 0 };

    multiply_matrix_4x4(a, b, result);

    for (int i = 0; i < 4; i++)
    {
        for (int j = 0; j < 4; j++)
        {
            printf("%d ", result[i][j]);
        }

        printf("\n");
    }

    return 0;
}