Arrays: Matrix multiplication

supriyo_biswas · updated April 11, 2020
#include <stdio.h>

int main() {
    // declare input and output arrays
    int a[10][10], b[10][10], c[10][10];

    // declare loop variables and variables to use
    // to store the number of rows and columns
    int a_rows, a_cols, b_rows, b_cols, i, j, k;

    // ask for the input matrix sizes
    printf("Enter the number of rows in 1st matrix:\n");
    scanf("%d", &a_rows);

    printf("Enter the number of columns in 1st matrix:\n");
    scanf("%d", &a_cols);

    // check if the number of rows/columns requested
    // in the 1st matrix is too big or too small
    if (a_rows < 1 || a_cols < 1 || a_rows > 10 || a_cols > 10) {
        printf("Invalid number of rows or columns\n");
        // exit the program
        return 0;
    }

    printf("Enter the number of rows in 2nd matrix:\n");
    scanf("%d", &b_rows);

    printf("Enter the number of columns in 2nd matrix:\n");
    scanf("%d", &b_cols);

    // check if the number of rows/columns requested
    // in the 2nd matrix is too big or too small
    if (b_rows < 1 || b_cols < 1 || b_rows > 10 || b_cols > 10) {
        printf("Invalid number of rows or columns\n");
        // exit the program
        return 0;
    }

    // check if matrix is compatible for multiplication
    if (a_cols != b_rows) {
        printf("Matrices are not compatible for multiplication\n");
        return 0;
    }

    // ask for the numbers
    printf("Enter the numbers of the first matrix\n");

    for (i = 0; i < a_rows; i++) {
        for (j = 0; j < a_cols; j++) {
            // array index starts from 0, but row or
            // column number starts from 1.
            printf("Enter row %i, column %i:\n", i + 1, j + 1);
            scanf("%d", &a[i][j]);
        }
    }

    printf("Enter the numbers of the second matrix\n");

    for (i = 0; i < b_rows; i++) {
        for (j = 0; j < b_cols; j++) {
            // array index starts from 0, but row or
            // column number starts from 1.
            printf("Enter row %i, column %i:\n", i + 1, j + 1);
            scanf("%d", &b[i][j]);
        }
    }

    // calculate the product of the matrices
    for (i = 0; i < a_rows; i++) {
        for (j = 0; j < b_cols; j++) {
            c[i][j] = 0;

            for (k = 0; k < a_cols; k++) {
                c[i][j] += a[i][k] * b[k][j];
            }
        }
    }

    printf("The first matrix is:\n");

    // print the first matrix
    for (i = 0; i < a_rows; i++) {
        for (j = 0; j < a_cols; j++) {
            printf("%d ", a[i][j]);
        }

        // to seperate the rows
        printf("\n");
    }

    printf("The second matrix is:\n");

    // print the second matrix
    for (i = 0; i < b_rows; i++) {
        for (j = 0; j < b_cols; j++) {
            printf("%d ", b[i][j]);
        }

        // to seperate the rows
        printf("\n");
    }

    printf("Product of the matrix is:\n");

    // print the product
    for (i = 0; i < a_rows; i++) {
        for (j = 0; j < b_cols; j++) {
            printf("%d ", c[i][j]);
        }

        // to seperate the rows
        printf("\n");
    }

    return 0;
}
Output
(Run the program to view its output)

Comments

Please sign up or log in to contribute to the discussion.