To find the difference of two Matrices

Here is the program to find the difference between the two integer matrices. The program asks for the order (M, N) of the matrices. Soon after entering the order, the cursor takes to the proper position in the screen to input the matrices. User need to hit ‘Enter’ button after each entry.

Logic : The logic applied here is very simple, that what we do in the paper. The first entry of the first matrix  ( A[1,1] ) is subtracted by the first entry of the second Matrix  (B[1,1]) and stored as the first member of the resultant matrix (C[1,1]). The procedure is continued till the last number of the matrix.

The procedure can be modified slightly with the same algorithm, to add, multiply, or transpose etc.

#include<stdio.h>
void main()
{
int A[5][5], B[5][5], C[5][5], i, j, m, n;
clrscr();
printf(“\n\n\t ENTER A ORDER OF THE MATRICES M,N…: “);
scanf(“%d,%d”, &m, &n);
printf(“\n\n\t ENTER THE ELEMENTS OF THE FIRST MATRIX..:\n\n”);
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
gotoxy(25+j*4,12+i*2);
scanf(“%d”,&A[i][j]);
}
printf(“\n”);
}
printf(“\n\t ENTER THE ELEMENTS OF THE SECOND MATRIX..:\n\n”);
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
gotoxy(25+j*4,18+m+i*2);
scanf(“%d”,&B[i][j]);
}
printf(“\n”);
}
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
C[i][j] = A[i][j] – B[i][j];
printf(“\n\t THE DIFFERENCE OF TWO MATRICES IS…:\n\n\t\t\t “);
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
printf(” %d”,C[i][j]);
printf(“\n\n\t\t\t “);
}
getch();
}
Download exe and source code here.

One Response to “To find the difference of two Matrices”

Leave a Reply