To find the sum of two Matrices

Thursday, December 4th, 2008

Here is the program to find the sum of 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 added up with 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 matrices.

The procedure can be modified slightly with the same algorithm, to subtract, 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(“nnt ENTER A ORDER OF THE MATRICES M,N…: “);
scanf(“%d,%d”, &m, &n);
printf(“nnt ENTER THE ELEMENTS OF THE FIRST MATRIX..:nn”);
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(“nt ENTER THE ELEMENTS OF THE SECOND MATRIX..:nn”);
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(“nt THE SUM OF TWO MATRICES IS…:nnttt “);
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
printf(” %d”,C[i][j]);
printf(“nnttt “);
}
getch();
}
Download exe and source code here.

Avatar Image

Author Name :
Ranjith

Total : 1 Comment


1 Response to To find the sum of two Matrices

  1. Anxia

    I want the addition of two matrices in java language

Leave a Reply

Question and Answer
C/C++ Unix & Linux Wordpress
Source codes
C C++ Java

Free email signup