Multiplication of two matrices

C program to multiply two matrices by passing matrix to a function.

To multiply two matrices, the number of columns of the first matrix should be equal to the number of rows of the second matrix.

As a result of multiplication, a new matrix is obtained that has the same quantity of rows as the 1st one has and the same quantity of columns as the 2nd one.

For example, if you multiply a matrix of 'n' x 'k' by 'k' x 'm' size you'll get a new one of 'n' x 'm' dimension.

To multiply two matrices, multiply the elements of each row of the first matrix by the elements of each column in the second matrix and add the products.

For example,

Program

#include<stdio.h>
#include<conio.h>
#define MAX 10

void read_matrix(int mat[MAX][MAX], int row, int col)
{
    int i, j;
    for(i=0;i<row;i++)
    {
        for(j=0;j<col;j++)
        {
            printf("Element [%d][%d]\t:",i,j);
            scanf("%d",&mat[i][j]);
        }
    }
}

void print_matrix(int mat[MAX][MAX], int row, int col)
{
    int i, j;
    for(i=0;i<row;i++)
    {
        for(j=0;j<col;j++)
        {
            printf("%d\t",mat[i][j]);
        }
        printf("\n");
    }
}

void mul_matrix(int mat1[MAX][MAX], int mat2[MAX][MAX], int mul[MAX][MAX], int row1, int col1, int col2)
{
    int i, j, k;
    for(i=0;i<row1;i++)
    {
        for(j=0;j<col2;j++)
        {
            mul[i][j] = 0;
            for(k=0;k<col1;k++)
            {
                mul[i][j] = mul[i][j] + (mat1[i][k] * mat2[k][j]);
            }
        }
    }
}

void main()
{
    int mat1[MAX][MAX], mat2[MAX][MAX], mul[MAX][MAX];
    int row1, col1, row2, col2;

    printf("Enter the number of rows of first matrix\t:");
    scanf("%d",&row1);
    printf("Enter the number of columns of first matrix\t:");
    scanf("%d",&col1);

    printf("Enter the number of rows of second matrix\t:");
    scanf("%d",&row2);
    printf("Enter the number of columns of second matrix\t:");
    scanf("%d",&col2);

    if(row2 == col1)
    {
        printf("Enter the elements of first matrix\n");
        read_matrix(mat1, row1, col1);
        printf("Enter the elements of second matrix\n");
        read_matrix(mat2, row2, col2);

        printf("\nFirst matrix entered is\n");
        print_matrix(mat1, row1, col1);
        printf("\nSecond matrix entered is\n");
        print_matrix(mat2, row2, col2);

        mul_matrix(mat1, mat2, mul, row1, col1, col2);

        printf("\nMatrix after multiplication\n");
        print_matrix(mul, row1, col2);
    }
    else
    {
        printf("\nMatrices cannot be multiplied");
    }

    getch();
}

Output

********** Run1 ********** 

Enter the number of rows of first matrix        :2
Enter the number of columns of first matrix     :3
Enter the number of rows of second matrix       :3
Enter the number of columns of second matrix    :2
Enter the elements of first matrix
Element [0][0]  :1
Element [0][1]  :2
Element [0][2]  :3
Element [1][0]  :4
Element [1][1]  :5
Element [1][2]  :6
Enter the elements of second matrix
Element [0][0]  :6
Element [0][1]  :5
Element [1][0]  :4
Element [1][1]  :3
Element [2][0]  :2
Element [2][1]  :1

First matrix entered is
1       2       3
4       5       6

Second matrix entered is
6       5
4       3
2       1

Matrix after multiplication
20      14
56      41


********** Run2 ********** 

Enter the number of rows of first matrix        :2
Enter the number of columns of first matrix     :3
Enter the number of rows of second matrix       :4
Enter the number of columns of second matrix    :3

Matrices cannot be multiplied