Subtract 'N' matrices

C program to subtract ā€˜Nā€™ matrices.

'N' matrices may be subtracted if and only if they all have the same dimension; i.e. they must have the same number of rows and columns.

Subtraction is accomplished by subtracting corresponding elements. 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 sub_matrix(int mat1[MAX][MAX], int mat2[MAX][MAX], int sub[MAX][MAX], int row, int col)
{
    int i, j;
    for(i=0;i<row;i++)
    {
        for(j=0;j<col;j++)
        {
            sub[i][j] = mat1[i][j] - mat2[i][j];
        }
    }
}

void main()
{
    int mat1[MAX][MAX], mat2[MAX][MAX], sub[MAX][MAX];
    int row, col, flag, i, j;
    char ch;

    printf("Enter the number of rows\t:");
    scanf("%d",&row);
    printf("Enter the number of columns\t:");
    scanf("%d",&col);

    printf("Enter the elements of first matrix\n");
    read_matrix(mat1, row, col);

    flag = 0;

    do
    {
        printf("\nEnter the elements of another matrix\n");
        read_matrix(mat2, row, col);

        if(flag == 1)
        {
            for(i=0;i<row;i++)
            {
                for(j=0;j<col;j++)
                {
                    mat1[i][j] = sub[i][j];
                }
            }
        }

        printf("\nFirst matrix is\n");
        print_matrix(mat1, row, col);
        printf("\nSecond matrix is\n");
        print_matrix(mat2, row, col);

        sub_matrix(mat1, mat2, sub, row, col);

        printf("\nMatrix after addition\n");
        print_matrix(sub, row, col);

        printf("\nDo you want to subtract another matrix in the result (y/n)\t:");
        fflush(stdin);
        scanf("%c",&ch);

        flag = 1;
    }
    while(ch == 'y' || ch == 'Y');

    getch();
}

Output

Enter the number of rows        :3
Enter the number of columns     :3
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
Element [2][0]  :7
Element [2][1]  :8
Element [2][2]  :9

Enter the elements of another matrix
Element [0][0]  :6
Element [0][1]  :5
Element [0][2]  :7
Element [1][0]  :8
Element [1][1]  :4
Element [1][2]  :3
Element [2][0]  :2
Element [2][1]  :1
Element [2][2]  :8

First matrix is
1       2       3
4       5       6
7       8       9

Second matrix is
6       5       7
8       4       3
2       1       8

Matrix after addition
-5      -3      -4
-4      1       3
5       7       1

Do you want to subtract another matrix in the result (y/n)      :y

Enter the elements of another matrix
Element [0][0]  :9
Element [0][1]  :8
Element [0][2]  :7
Element [1][0]  :6
Element [1][1]  :5
Element [1][2]  :4
Element [2][0]  :3
Element [2][1]  :2
Element [2][2]  :1

First matrix is
-5      -3      -4
-4      1       3
5       7       1

Second matrix is
9       8       7
6       5       4
3       2       1

Matrix after addition
-14     -11     -11
-10     -4      -1
2       5       0

Do you want to subtract another matrix in the result (y/n)      :n