Add two matrices using pointers

C Program to add 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], add[MAX][MAX];
    int rows, cols;
    int i, j;

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

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

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

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

    for(i=0;i<rows;i++)
    {
        for(j=0;j<cols;j++)
        {
            *(*(add + i) + j) = *(*(mat1 + i) + j) + *(*(mat2 + i) + j);
        }
    }

    printf("\nFirst Matrix\n");
    print_matrix(mat1, rows, cols);

    printf("\nSecond Matrix\n");
    print_matrix(mat2, rows, cols);

    printf("\nAddition Matrix\n");
    print_matrix(add, rows, cols);

    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 matrix  :3
Enter the number of columns in the matrix       :3

Enter elements in the first matrix
1
23
21
34
54
23
1
2
3

Enter elements in the second matrix
4
54
34
23
12
23
21
34
5

First Matrix
  1  23  21
 34  54  23
  1   2   3

Second Matrix
  4  54  34
 23  12  23
 21  34   5

Addition Matrix
  5  77  55
 57  66  46
 22  36   8

Explanation