Starting Vocational Training From 1-May-2024 Get Detail
#include <stdio.h>
#define MAXROW 10
#define MAXCOL 10
/*User Define Function to Read Matrix*/
void readMatrix(int m[][MAXCOL],int row,int col)
{
int i,j;
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
printf("Enter element [%d,%d] : ",i+1,j+1);
scanf("%d",&m[i][j]);
}
}
}
/*User Define Function to Read Matrix*/
void printMatrix(int m[][MAXCOL],int row,int col)
{
int i,j;
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
printf("%d ",m[i][j]);
}
printf("
");
}
}
int main()
{
int a[MAXROW][MAXCOL],b[MAXROW][MAXCOL],result[MAXROW][MAXCOL];
int i,j,r1,c1,r2,c2;
printf("Enter number of Rows of matrix a: ");
scanf("%d",&r1);
printf("Enter number of Cols of matrix a: ");
scanf("%d",&c1);
printf("
Enter elements of matrix a:
");
readMatrix(a,r1,c1);
printf("Enter number of Rows of matrix b: ");
scanf("%d",&r2);
printf("Enter number of Cols of matrix b: ");
scanf("%d",&c2);
printf("
Enter elements of matrix b:
");
readMatrix(b,r2,c2);
/*sum and sub of Matrices*/
if(r1==r2 && c1==c2)
{
/*Adding two matrices a and b, and result storing in matrix result*/
for(i=0;i< r1;i++)
{
for(j=0;j< c1;j++)
{
result[i][j]=a[i][j]+b[i][j];
}
}
/*print matrix*/
printf("
Matrix after adding (result matrix):
");
printMatrix(result,r1,c1);
/*Subtracting two matrices a and b, and result storing in matrix result*/
for(i=0;i< r1;i++)
{
for(j=0;j< c1;j++)
{
result[i][j]=a[i][j]-b[i][j];
}
}
/*print matrix*/
printf("
Matrix after subtracting (result matrix):
");
printMatrix(result,r1,c1);
}
else
{
printf("
Matrix can not be added, Number of Rows & Cols are Different");
}
return 0;
}