Input two
Matrix, store the sum to an another Matrix.
#include <stdio.h>
#include <conio.h>
#define MAX 10
/* Matrix Addition*/
void
disp(int x[][MAX],int r,int c)
{
int
i,j;
for(i=0;i<r;i++)
{
printf("\t");
for(j=0;j<c;j++)
{
printf("%3d
",x[i][j]);
}
printf("\n");
}
}
void input(int x[][MAX],int r,int c)
{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("\n\tElement
At [%d,%d] ; ",i,j);
scanf("%d",&x[i][j]);
}
}
}
void
madd(int x[][MAX],int y[][MAX],int z[][MAX],int r,int c)
{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
z[i][j]=x[i][j]+y[i][j];
}
}
}
void
main()
{
int
a[MAX][MAX],b[MAX][MAX],c[MAX][MAX],n,m;
clrscr();
printf("\n\tEnter
Row Size [Below 10] : ");
scanf("%d",&n);
printf("\n\tEnter
Column Size [Below 10] : ");
scanf("%d",&m);
printf("\n\tFirst
Matrix : \n");
input(a,n,m);
printf("\n\tSecond
Matrix : \n");
input(b,n,m);
printf("\n\tFirst
Matrix : \n");
disp(a,n,m);
madd(a,b,c,n,m);
printf("\n\tSecond
Matrix : \n");
disp(b,n,m);
printf("\n\tResultant
Matrix : \n");
disp(c,n,m);
getch();
}
Input two Matrix, Multiply both
and store in a third Matrix.
#include <stdio.h>
#include <conio.h>
/* Matrix Multiplication */
#define MAX 10
void
disp(int x[][MAX],int r,int c)
{
int
i,j;
for(i=0;i<r;i++)
{
printf("\t");
for(j=0;j<c;j++)
{
printf("%3d
",x[i][j]);
}
printf("\n");
}
}
void input(int x[][MAX],int r,int c)
{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("\n\tElement
At [%d,%d] ; ",i,j);
scanf("%d",&x[i][j]);
}
}
}
void mmult(int x[][MAX],int y[][MAX],int
z[][MAX],int r,int c,int r1)
{
int i,j,k,s;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
s=0;
for(k=0;k<r1;k++)
{
s=s+x[i][k]*y[k][j];
}
z[i][j]=s;
}
}
}
void
main()
{
int
a[MAX][MAX],b[MAX][MAX],c[MAX][MAX],n,m,n1,m1;
clrscr();
printf("\n\tEnter
Row Size for First Matrix [Below 10] : ");
scanf("%d",&n);
printf("\n\tEnter
Column Size for First Matrix [Below 10] : ");
scanf("%d",&m);
printf("\n\tEnter
Row Size for Second Matrix [Below 10] : ");
scanf("%d",&n1);
printf("\n\tEnter
Column Size for Second Matrix [Below 10] : ");
scanf("%d",&m1);
if(n==n1 || n==m1)
{
printf("\n\tFirst
Matrix : \n");
input(a,n,m);
printf("\n\tSecond
Matrix : \n");
input(b,n1,m1);
mmult(a,b,c,n,m1,n1);
printf("\n\tFirst
Matrix : \n");
disp(a,n,m);
printf("\n\tSecond
Matrix : \n");
disp(b,n1,m1);
printf("\n\tResultant
Matrix : \n");
disp(c,n,m1);
}
else
{
printf("\n\tInvalid
Size for Multiplication..");
}
getch();
}