#include <stdio.h>
#include <stdlib.h>

// Define constants for matrix dimensions
#define M 3 // Number of rows in matrix A
#define N 2 // Number of columns in matrix A and rows in matrix B
#define P 3 // Number of columns in matrix B

void print_array(int r, int c, int arr[r][c]);
void print_array2(int arr[][N]);

// int C[M][P];

int main()
{
    // Can array be reassigned?
    // int a[2] = {1,2};
    // int s[2];
    // s = a;

    // Initialize matrix A (3x2)
    int A[M][N] =
    {
        {1, 2},
        {3, 4},
        {5, 6}
    };

    // Initialize matrix B (2x3)
    int B[N][P] =
    {
        {7, 8, 9},
        {10, 11, 12}
    };

    // Declare matrix C (3x3) to store the result of A x B
    // int C[M][P];

    // Step 1: Initialize matrix C to zero
    // Matrix C will store the result of the multiplication, so we zero out all elements
    // for (int i = 0; i < M; ++i)
    //     for (int j = 0; j < P; ++j)
    //         C[i][j] = 0;

    // C[M][P] = {0};

    //Partial initialisation
    int C[M][P] = {0};


    // Step 2: Multiply matrices A and B, and store the result in matrix C
    // The formula for matrix multiplication is:
    // C[i][j] = sum(A[i][k] * B[k][j]) for all k
    for (int i = 0; i < M; ++i)         // Loop over rows of matrix A
    {
        for (int j = 0; j < P; ++j)     // Loop over columns of matrix B
        {
            for (int k = 0; k < N; ++k) // Loop over columns of A and rows of B
            {
                // Accumulate the product of corresponding elements from A and B
                C[i][j] += A[i][k] * B[k][j];
            }
        }
    }

    // Step 3: Display the resulting matrix C (3x3)
    printf("Resultant Matrix C (%dx%d):\n", M, P);
    for (int i = 0; i < M; ++i)
    {
        for (int j = 0; j < P; ++j)
            // Print each element of matrix C with a field width of 5 for neat formatting
            printf("%5d", C[i][j]); 
        printf("\n"); // Move to the next row after each row is printed
    }
    print_array(M,P,C);

    return 0; // Indicate successful execution
}

void print_array(int r, int c, int arr[r][c]){
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
            printf("%5d", arr[i][j]);
        }
        printf("\n");
    }
}