C program to multiply two matrices

Last updated on 07th February 2023

Matrix multiplication is a binary operation that multiplies a matrix A of size m x n and another matrix B of size n x p to produce a result matrix C of size m x p where each element Cij is given by:

Cij = Σ(Aik * Bkj), where k = 1 to n.

Here, Aik is an element from the ith row and kth column of matrix A, and Bkj is an element from the kth row and jth column of matrix B.

So, in order to multiply two matrices, we take the dot product of each row of the first matrix with each column of the second matrix. The dot product is the sum of the products of the corresponding elements.

Consider two matrices - matrix A of size 2 x 3 and matrix B of size 3 x 2

Matrix Multiplication

The product of matrices A and B is matrix C of size 2 x 2

Product Matrix

Elements of matrix C are calculated as follows.

C11 = 5*2 + 3*3 + 9*1 = 28	
Multiplication Result

Similarly,

C12 = 5*5 + 3*8 + 9*6 = 103				
C21 = 2*2 + 4*3 + 1*1 = 17				
C22 = 2*5 + 4*8 + 1*6 = 48				

Thus we get the result matrix C as

Matrix Product

C program to multiply two matrices

Here is a basic C program to multiply two matrices.

#include <stdio.h>
#define MAX 10

int main() {
    int row1, col1, row2, col2;
    int a[MAX][MAX], b[MAX][MAX], result[MAX][MAX];
    
    // Read first matrix
    printf("Enter rows and columns for first matrix: ");
    scanf("%d%d", &row1, &col1);
    printf("Enter elements in the first matrix:\n");
    for (int i = 0; i < row1; i++)
        for (int j = 0; j < col1; j++)
            scanf("%d", &a[i][j]);

    // Read second matrix
    printf("Enter rows and columns for second matrix: ");
    scanf("%d%d", &row2, &col2);
    printf("Enter elements in the second matrix:\n");
    for (int i = 0; i < row2; i++)
        for (int j = 0; j < col2; j++)
            scanf("%d", &b[i][j]);

    // Check if multiplication is possible
    if (col1 != row2) {
        printf("Cannot multiply.\n");
        return 0;
    }
    
    //Initialize the result matrix to 0
     for (int i = 0; i < row1; i++)
        for (int j = 0; j < col2; j++) 
            result[i][j] = 0;
            
    // Multiply the two matrices
    for (int i = 0; i < row1; i++)
        for (int j = 0; j < col2; j++)
            for (int k = 0; k < col1; k++)
                result[i][j] += a[i][k] * b[k][j];
    
    // Print result matrix
    printf("The product of the two matrices is \n");
    for (int i = 0; i < row1; i++){
        for (int j = 0; j < col2; j++) 
            printf("%d ", result[i][j]);
        printf("\n");
    }
    return 0;
}

This program prompts the user to input the number of rows and columns and then reads the elements of that matrix. After reading two matrices, it checks if the number of columns in the first matrix is equal to the number of rows in the second matrix. If not, the program will display the message Cannot multiply and ends.

Next, it initializes the result matrix to zero, multiplies the two matrices and finally prints the result.

Output from the above program will be like this.

Enter rows and columns for first matrix: 2 3
Enter elements in the first matrix:
5 3 9
2 4 1
Enter rows and columns for second matrix: 3 2
Enter elements in the second matrix:
2 5
3 8
1 6
The product of the two matrices is 
28 103 
17 48 

Post a comment

Comments

Nothing yet..be the first to share wisdom.