Skip to content
Laviyx Learn

Try

PPS Lab · Experiment 20

Multiplication of two matrices using functions

CArrays, pointers and functionsJNTUH R22 · CS108ES

Aim

To write a C program that reads two matrices, checks that they can be multiplied, and uses functions to read them, multiply them and print the product matrix.

Algorithm

  1. Read the order r1 × c1 of matrix A and the order r2 × c2 of matrix B. Each value must be between 1 and 10.
  2. If c1 is not equal to r2, the matrices cannot be multiplied. Print a message and stop.
  3. Call read_matrix to read the elements of A and then of B.
  4. Call multiply_matrices: for each row i of A and each column j of B, set C[i][j] = 0, then for k = 0 to c1 − 1 add A[i][k] × B[k][j] to C[i][j].
  5. Call print_matrix to print the r1 × c2 product matrix C.

Program

C
#include <stdio.h>

#define MAX 10

int read_matrix(int m[MAX][MAX], int rows, int cols) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            if (scanf("%d", &m[i][j]) != 1) {
                return 0;
            }
        }
    }
    return 1;
}

int read_order(int *rows, int *cols) {
    if (scanf("%d %d", rows, cols) != 2) {
        return 0;
    }
    return *rows >= 1 && *rows <= MAX && *cols >= 1 && *cols <= MAX;
}

/* C (r1 x c2) = A (r1 x c1) x B (c1 x c2) */
void multiply_matrices(int a[MAX][MAX], int b[MAX][MAX], int c[MAX][MAX],
                       int r1, int c1, int c2) {
    for (int i = 0; i < r1; i++) {
        for (int j = 0; j < c2; j++) {
            c[i][j] = 0;
            for (int k = 0; k < c1; k++) {
                c[i][j] += a[i][k] * b[k][j];
            }
        }
    }
}

void print_matrix(int m[MAX][MAX], int rows, int cols) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("%5d", m[i][j]);
        }
        printf("\n");
    }
}

int main(void) {
    int a[MAX][MAX], b[MAX][MAX], c[MAX][MAX];
    int r1, c1, r2, c2;

    printf("Enter rows and columns of matrix A: ");
    if (!read_order(&r1, &c1)) {
        printf("Rows and columns must be between 1 and %d\n", MAX);
        return 1;
    }
    printf("Enter rows and columns of matrix B: ");
    if (!read_order(&r2, &c2)) {
        printf("Rows and columns must be between 1 and %d\n", MAX);
        return 1;
    }
    if (c1 != r2) {
        printf("Cannot multiply: columns of A (%d) must equal rows of B (%d)\n", c1, r2);
        return 1;
    }

    printf("Enter the elements of matrix A (%d x %d):\n", r1, c1);
    if (!read_matrix(a, r1, c1)) {
        printf("Invalid input\n");
        return 1;
    }
    printf("Enter the elements of matrix B (%d x %d):\n", r2, c2);
    if (!read_matrix(b, r2, c2)) {
        printf("Invalid input\n");
        return 1;
    }

    multiply_matrices(a, b, c, r1, c1, c2);

    printf("Product matrix A x B (%d x %d):\n", r1, c2);
    print_matrix(c, r1, c2);
    return 0;
}

Output

Compatible orders (2 x 3 and 3 x 2)

Output 1
Enter rows and columns of matrix A: 2 3
Enter rows and columns of matrix B: 3 2
Enter the elements of matrix A (2 x 3):
1 2 3
4 5 6
Enter the elements of matrix B (3 x 2):
7 8
9 10
11 12
Product matrix A x B (2 x 2):
   58   64
  139  154

Orders that cannot be multiplied

Output 2
Enter rows and columns of matrix A: 2 3
Enter rows and columns of matrix B: 2 2
Cannot multiply: columns of A (3) must equal rows of B (2)

Recorded by compiling and running this exact program. Typed input appears after each prompt.

How it works

A × B is defined only when the number of columns of A equals the number of rows of B. If A is r1 × c1 and B is c1 × c2, the product is r1 × c2.

C[i][j] must be set to 0 before the k loop adds to it, because local arrays in C start with garbage values.

read_order takes pointers to rows and cols so that it can store the two values it reads in main's variables.

Common mistakes

  • Not setting c[i][j] = 0 before adding the products. The sum then starts from a garbage value and the answer is wrong.

  • Checking r1 == r2 or c1 == c2 instead of c1 == r2. The rule for multiplication is columns of A = rows of B.

  • Writing c[i][j] = a[i][j] * b[i][j]. That multiplies matching elements; it is not matrix multiplication. Each element of C is a sum over k of a[i][k] * b[k][j].

  • Using the wrong limit for the k loop. It must run over the common size c1 (= r2), not over r1 or c2.

Viva questions

Try to answer each one aloud before you open it.

  • When can two matrices be multiplied?

    When the number of columns of the first matrix equals the number of rows of the second. If A is m × n and B is n × p, the product is m × p.

  • Is matrix multiplication commutative?

    No. In general A × B is not equal to B × A, and one of them may not even be defined.

  • How many multiplications does this program do for an m × n matrix times an n × p matrix?

    m × n × p multiplications, because each of the m × p elements needs n multiplications.

  • Why are there three nested loops?

    The outer two loops choose the element C[i][j]. The inner loop adds the n products of row i of A and column j of B for that element.

  • What is the order of the product of a 2 × 3 matrix and a 3 × 2 matrix?

    2 × 2.

Learn the concepts

All Programming for Problem Solving Lab experiments →