Write a program that reads two matrices and determines their
Write a program that reads two matrices and determines their product matrix.
For example:
1 2 3 1 2 3 4 5
M1 = and M2= 2 5 3 1 4
3 2 1 5 4 3 2 1
then
20 24 18 12 16
P =
12 20 18 16 24
Requirements:
Implement the following functions:
void readm(matrix [ ] [ ], int rows, int cols);
void writem(matrix[ ][ ], int rows, int cols);
void mulmatrix(float matrix1 [ ] [ ], int rows1, int cols1, matrix2 [ ] [ ], int cols2, float product [ ] [ ]);
Input the row size and column size for matrix M1 and M2
Input the matrix
Validate the product matrix dimension
Hint:
void mulmatrix(float matrix1[ ] [ ], int rows1, int cols1, float matrix2[ ] [ ] , int cols2, float product[ ] )
{int i, j, k;
for (i = 0; ________________ )
for(j=0;________________ )
{
for(k = 0, *product = 0;________________________)
/* p[i,j] += m1[i,k] * m2[k j]*/
Solution
C++ CODE
#include<iostream>
using namespace std;
#define MAX_ROW_SIZE 50
#define MAX_COL_SIZE 50
// Function Declarations
void readm(float matrix[MAX_ROW_SIZE][MAX_COL_SIZE],int rows,int cols); // function to read the matrix from the user
void writem(float matrix[MAX_ROW_SIZE][MAX_COL_SIZE],int rows,int cols);// function to display the contents of a matrix
// function to find the product of two matrix
void mulmatrix(float matrix1[MAX_ROW_SIZE][MAX_COL_SIZE], int row1, int cols1, float matrix2[MAX_ROW_SIZE][MAX_COL_SIZE], int cols2, float product[MAX_ROW_SIZE][MAX_COL_SIZE]);
// the main function
int main()
{
int row1,cols1,row2,cols2; // variable decleration
float mat1[MAX_ROW_SIZE][MAX_COL_SIZE],mat2[MAX_ROW_SIZE][MAX_COL_SIZE],product[MAX_ROW_SIZE][MAX_COL_SIZE]; // decleration of the matrices
// Reading the informations of the size of the first matrix
cout << \"Enter the size of the first matrix\"<< endl << \"ROW :\";
cin >> row1;
cout << \"COLUMN :\";
cin >> cols1;
// Read the details of the second matrix
cout << \"Enter the size of the second mattrix\"<< endl << \"ROW :\";
cin >> row2;
cout << \"COLUMN :\";
cin >> cols2;
cout << \"Enter the first matrix\"<< endl;
readm(mat1,row1,cols1); // call the function to scan the matrix
cout <<\" M1 = \" << endl;
writem(mat1,row1,cols1); // print the first matrix to the screen
cout <<\"Enter the second matrix\" << endl;
readm(mat2,row2,cols2); // reading the second matrix
cout <<\"M2 = \" << endl;
writem(mat2,row2,cols2); // print the second matrix to the screen
// check the possiblity of multiplication
if(cols1 ==row2) // In order to multiply the cols1 should be same as row2
{
mulmatrix(mat1,row1,cols1,mat2,cols2,product); // call the function mulmatrix() to compute the product
cout <<\"Product = \" << endl;
writem(product,row1,cols2); // print the results to the screen
}
else
{
cout << \"The matrix can not be multiplied.\"<< endl;
}
return 0;
}
void readm(float matrix[MAX_ROW_SIZE][MAX_COL_SIZE],int rows,int cols)
{
for(int i=0;i<rows;i++) // loop for the row indexing
for(int j=0;j<cols;j++) // loop for the column indexing
cin >> matrix[i][j]; // copying the element as the ij th entry of the matrix
}
void writem(float matrix[MAX_ROW_SIZE][MAX_COL_SIZE],int rows,int cols)
{
for(int i=0;i<rows;i++) // loop for the row
{
for(int j=0;j<cols;j++) // loop for the column
{
cout << matrix[i][j] << \"\\t\" ; // print the elements as a matrix form
}
cout << endl;
}
}
// function to find the product of the matrix
void mulmatrix(float matrix1[MAX_ROW_SIZE][MAX_COL_SIZE], int row1, int cols1, float matrix2[MAX_ROW_SIZE][MAX_COL_SIZE], int cols2, float product[MAX_ROW_SIZE][MAX_COL_SIZE])
{
for(int i =0;i<row1;i++) // loop for the row of product matrix
for(int j=0;j<cols2;j++) // loop for the column of the product matrix
for(int k=0;k<cols1;k++) // loop for the summation index
product[i][j] += matrix1[i][k]*matrix2[k][j]; // finding the product
}
SAMPLE OUTPUT
maths@maths-HP-Z440-Workstation:~/Chegg$ g++ 15825085.cpp
maths@maths-HP-Z440-Workstation:~/Chegg$ ./a.out
Enter the size of the first matrix
ROW :2
COLUMN :3
Enter the size of the second mattrix
ROW :3
COLUMN :5
Enter the first matrix
1
2
3
3
2
1
M1 =
1 2 3
3 2 1
Enter the second matrix
1
2
3
4
5
2
5
3
1
4
5
4
3
2
1
M2 =
1 2 3 4 5
2 5 3 1 4
5 4 3 2 1
Product =
20 24 18 12 16
12 20 18 16 24
maths@maths-HP-Z440-Workstation:~/Chegg$


