Multiply two matrices using pointers

C Program to multiply two matrices using pointer

Program

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

void print_matrix(int mat[MAX][MAX], int rows, int cols);
void input_matrix(int mat[MAX][MAX], int rows, int cols);

void main()
{
	int mat1[MAX][MAX], mat2[MAX][MAX], mul[MAX][MAX];
    int rows1, cols1, rows2, cols2;
    int i, j, k;

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

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

    rows2 = cols1;
    printf("number of rows in second matrix = number of columns in first matrix = %d\n",cols1);

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

    printf("\nEnter elements in the first matrix\n");
    input_matrix(mat1, rows1, cols1);

    printf("\nEnter elements in the second matrix\n");
    input_matrix(mat2, rows2, cols2);

    for(i=0;i<rows1;i++)
    {
        for(j=0;j<cols2;j++)
        {
            *(*(mul + i) + j) = 0;
            for(k=0;k<cols1;k++)
            {
                *(*(mul + i) + j) = *(*(mul + i) + j) + (*(*(mat1 + i) + k) * *(*(mat2 + k) + j));
            }
        }
    }

    printf("\nFirst Matrix\n");
    print_matrix(mat1, rows1, cols1);

    printf("\nSecond Matrix\n");
    print_matrix(mat2, rows2, cols2);

    printf("\nMultiplication Matrix\n");
    print_matrix(mul, rows1, cols2);

    getch();
}

void input_matrix(int mat[MAX][MAX], int rows, int cols)
{
    int i, j;
    for(i=0;i<rows;i++)
    {
        for(j=0;j<cols;j++)
        {
            scanf("%d",(*(mat+i)+j));
        }
    }
}

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

Output

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

Enter elements in the first matrix
1
2
3
4
5
6

Enter elements in the second matrix
6
5
4
3
2
1

First Matrix
  1   2   3
  4   5   6

Second Matrix
  6   5
  4   3
  2   1

Multiplication Matrix
 20  14
 56  41

Explanation