Write a C program for Matrix Multiplication using a 2D Array.

Function Main
    Declare Integer Array A[2*2], B[2*2], C[2*2]
    Assign A[2*2] = {{0.866, -0.500}, {0.500, 0.866}}
    Assign B[2*2] = {{0.500, -0.866}, {0.866, 0.500}}
    Declare Integer i, j, k
    For i = 0 to 2-1
        For j = 0 to 2-1
            Assign C [i*j] = 0
            For k = 0 to 2-1
                Assign C[i*j] = C[i*j] + A[i*k] * B [k*j]
            End
        End
    End
    For i = 0 to 2-1
        For j = 0 to 2-1
            Output " " &C[i*j]
        End
        Output "Newline"
    End
End

#include <stdio.h>
int main() {
    //! A = showArray2D(A, rowCursors=[i], colCursors=[k], width=.33)
    //! B = showArray2D(B, rowCursors=[k], colCursors=[j], width=.33)
    //! C = showArray2D(C, rowCursors=[i], colCursors=[j], width=.33)
    double A[2][2] = {{0.866, -0.500}, {0.500, 0.866}};
    double B[2][2] = {{0.500, -0.866}, {0.866, 0.500}};
    double C[2][2];
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            C[i][j] = 0;
            for (int k = 0; k < 2; k++) {
                C[i][j] += A[i][k] * B[k][j];
            }
        }
    }
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            printf("%.3f ", C[i][j]);
        }
        printf("\n");
    }
    return 0;
}

0.000 -1.000                                                                    
1.000 0.000  

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.