I need help in this program about matrix in C Write three matrix math functions:
ID: 3808028 • Letter: I
Question
I need help in this program about matrix in C
Write three matrix math functions: add Matrix which will add two matrices such that result = matrix1 + matrix2 sub Matrix which will subtract two matrices such that result = matrix1 - matrix2 molt Matrix which will multiply two matrices such that result = matrix1 x matrix2 Given the following source matrices: The function add Matrix produces the following matrix as its result: The function subMatrix produces the following matrix as its result: The function multMatrix produces the following matrix as its result: M1 times M2 = [75.84 108.05 89.24 93.31 124.51 141.59 133.13 183.26 179.25] The prototypes for the three functions must be: void addMatrix(const double matrixl[][3], const double matrix2[][31, double result[][3], lnt rows); void subMatrix(const double matrixl[][3], const double matrlx2[][3], double result[][3], lnt rows); void multMatrix(const double matrixl[3][3], const double matrlx2[3] [3], double result [3] [3]); The addMatrix and subMatrix functions will handle matrices with any number of rows - the last argument (rows) tells the function the number of rows. All matrices that these functions handle will have three columns. The multMatrix function will multiply a 3 times 3 matrix by another 3 times 3 matrix to produce a result matrix that will also be 3 times 3. You will need to write a driver to test your functions. We will test your functions with our driver Our driver will call your functions as specified by the prototypes as given above.Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
void addMatrix(const double matrix1[][3],const double matrix2[][3],double result[][3],int row)
{
int i,j;
for(i=0;i<row;i++)
{
for(j=0;j<3;j++)
{
result[i][j]=matrix1[i][j]+matrix2[i][j];
}
}
printf("Addition of two matrix is :- ");
for(i=0;i<row;i++)
{
printf(" ");
for(j=0;j<3;j++)
{
printf("%.1lf ",result[i][j]);
}
}
printf(" ");
}
void subMatrix(const double matrix1[][3],const double matrix2[][3],double result[][3],int row)
{
int i,j;
for(i=0;i<row;i++)
{
for(j=0;j<3;j++)
{
result[i][j]= matrix1[i][j] - matrix2[i][j];
}
}
printf("Subtraction of two matrix is :- ");
for(i=0;i<row;i++)
{
printf(" ");
for(j=0;j<3;j++)
{
printf("%.1lf ",result[i][j]);
}
}
printf(" ");
}
void mulMatrix(const double matrix1[3][3],const double matrix2[3][3],double result[3][3])
{
int i,j,k;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(k = 0; k < 3; k++)
result[i][j]+= matrix1[i][k] * matrix2[k][j];
}
}
printf("Multiplication of two matrix is :- ");
for(i=0;i<3;i++)
{
printf(" ");
for(j=0;j<3;j++)
{
printf("%.1lf ",result[i][j]);
}
}
printf(" ");
}
int main()
{
int row;
int i,j;
const double M1[][3]={{5.5,7.3,8.6},{6.4,9.3,13.4},{12.3,12.5,5.8}};
const double M2[][3]={{1.7,3.6,2.3},{3.1,6.3,9.2},{5.1,3.2,6.2}};
double result[][3]={0};
printf("Enter the row : ");
scanf("%d",&row);
printf("matrix 1 (M1) is :- ");
for(i=0;i<3;i++)
{
printf(" ");
for(j=0;j<3;j++)
{
printf("%.1lf ",M1[i][j]);
}
}
printf(" matrix 2 (M2) is :- ");
for(i=0;i<3;i++)
{
printf(" ");
for(j=0;j<3;j++)
{
printf("%.1lf ",M2[i][j]);
}
}
int choice;
printf(" Enter Your Choice 1. AddMatrix 2.SubMatrix 3.MulMatrix : ");
scanf("%d",&choice);
switch(choice)
{
case 1:addMatrix(M1,M2,result,row); break;
case 2:subMatrix(M1,M2,result,row); break;
case 3:mulMatrix(M1,M2,result); break;
default : printf("Wrong Entry !!");
}
return 0;
}
---------------------------------------
output sample 1:-
Enter the row : 3
matrix 1 (M1) is :-
5.5 7.3 8.6
6.4 9.3 13.4
12.3 12.5 5.8
matrix 2 (M2) is :-
1.7 3.6 2.3
3.1 6.3 9.2
5.1 3.2 6.2
Enter Your Choice 1. AddMatrix 2.SubMatrix 3.MulMatrix : 1
Addition of two matrix is :-
7.2 10.9 10.9
9.5 15.6 22.6
17.4 15.7 12.0
-----------------------------
output sample 2:-
Enter the row : 3
matrix 1 (M1) is :-
5.5 7.3 8.6
6.4 9.3 13.4
12.3 12.5 5.8
matrix 2 (M2) is :-
1.7 3.6 2.3
3.1 6.3 9.2
5.1 3.2 6.2
Enter Your Choice 1. AddMatrix 2.SubMatrix 3.MulMatrix : 2
Subtraction of two matrix is :-
3.8 3.7 6.3
3.3 3.0 4.2
7.2 9.3 -0.4
---------------------------------------------------------------------------------------------
If you have any query, please feel free to ask.
Thanks a lot.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.