Addition of two matrices
Algorithm
Input Two matrices a and b Output Output matrix c containing elements after addition of a and b complexity O(n^2) Matrix-Addition(a,b) 1 for i =1 to rows [a] 2 for j =1 to columns[a] 3 Input a[i,j]; 4 Input b[i,j]; 5 C[i, j] = A[i, j] + B[i, j]; 6 Display C[i,j];
Algorithm Description
To add two matrixes sufficient and necessary condition is "dimensions of matrix A = dimensions of matrix B". Loop for number of rows in matrix A. Loop for number of columns in matrix A. Input A[i,j] and Input B[i,j] then add A[i,j] and B[i,j] store and display this value as C[i,j];
C Code
//Addition of Two Matrices
//Input : Two matrices of type integer
#include< stdio.h>
#include< conio.h>
int main()
{
int a[10][10],b[10][10],c[10][10],i,j,r1,c1;
printf("Enter rows n columns of matrix A and B ");
scanf("%d %d",&r1,&c1);
printf("Enter elements for matrix A and B\n");
for(i=0;i< r1;i++)
{
for(j=0;j< c1;j++)
{
scanf("%d",&a[i][j]);
scanf("%d",&b[i][j]);
}
}
printf("Matrix addition is :\n");
for(i=0;i< r1;i++)
{
for(j=0;j< c1;j++)
{
c[i][j]=a[i][j]+b[i][j];
printf("%d\t",c[i][j]);
}
printf("\n");
}
}