Write a function named matrix_multiply which has input arguments two matrices an
ID: 3795227 • Letter: W
Question
Write a function named matrix_multiply which has input arguments two matrices and returns a result which is the product of the two matrices (in the order listed). If the matrices cannot be multiplied the function should print an informative error message and return an empty matrix. Use loop(s) to perform the matrix multiplication. DO NOT USE MATLAB MATRIX MULTIPLICATION. Test the function using C = matrix_multiply ([1 2;3 4], [5 6;7 8]) and D = matrix_multiply([2;3], [4 51) and E = matrix_multiply([1 2], [3 4]) Write a script to test the matrix multiply function. The script should prompt the user for the number of tests to be run and for each test generate two random matrices (of appropriate sizes so they can be multiplied) then (a) multiply them using the matrix_multiply function and (b) multiply them using the MATLAB matrix multiplication and compare the results. For each test, if the results arc equal output a single-line "test passed" message along with the sizes of the matrices and if the results are not equal output a "test failed" message and print the sizes, matrices, and results obtained. Note that a tolerance may be needed for the comparison due to round-off errors. Test the script two ways, one with a "broken" matrix_multiply function, as described in class.Explanation / Answer
#include <iostream>
using namespace std;
void enterElement(int fMat[][10], int sMat[][10], int rFirst, int cFirst, int rsecond, int csecond);
void matrix_multiply(int fMat[][10], int sMat[][10], int multResult[][10], int rFirst, int cFirst, int rsecond, int csecond);
void display(int mult[][10], int rFirst, int csecond);
int main()
{
int fMat[10][10], sMat[10][10], mult[10][10], rFirst, cFirst, rsecond, csecond, i, j, k;
cout << "Enter rows and column for first matrix: ";
cin >> rFirst >> cFirst;
cout << "Enter rows and column for second matrix: ";
cin >> rsecond >> csecond;
// If column of first matrix in not equal to row of second matrix,return zero matrix
while (cFirst != rsecond)
{
cout << "Error! column of first matrix not equal to row of second." << endl;
cout << " [0] ";
}
// Function to take matrices elements
enterElement(fMat, sMat, rFirst, cFirst, rsecond, csecond);
// Function to multiply two matrices.
matrix_multiply(fMat, sMat, mult, rFirst, cFirst, rsecond, csecond);
// Function to display resultant matrix after multiplication.
display(mult, rFirst, csecond);
return 0;
}
void enterElement(int fMat[][10], int sMat[][10], int rFirst, int cFirst, int rsecond, int csecond)
{
int i, j;
cout << endl << "Enter elements of matrix 1:" << endl;
for(i = 0; i < rFirst; ++i)
{
for(j = 0; j < cFirst; ++j)
{
cout << "Enter elements a"<< i + 1 << j + 1 << ": ";
cin >> fMat[i][j];
}
}
cout << endl << "Enter elements of matrix 2:" << endl;
for(i = 0; i < rsecond; ++i)
{
for(j = 0; j < csecond; ++j)
{
cout << "Enter elements b" << i + 1 << j + 1 << ": ";
cin >> sMat[i][j];
}
}
}
void matrix_multiply(int fMat[][10], int sMat[][10], int mult[][10], int rFirst, int cFirst, int rsecond, int csecond)
{
int i, j, k;
// Initializing elements of matrix mult to 0.
for(i = 0; i < rFirst; ++i)
{
for(j = 0; j < csecond; ++j)
{
mult[i][j] = 0;
}
}
// Multiplying matrix fMatrix and sMatrix and storing in array mult.
for(i = 0; i < rFirst; ++i)
{
for(j = 0; j < csecond; ++j)
{
for(k=0; k<cFirst; ++k)
{
mult[i][j] += fMat[i][k] * sMat[k][j];
}
}
}
}
void display(int mult[][10], int rFirst, int csecond)
{
int i, j;
cout << "Output Matrix:" << endl;
for(i = 0; i < rFirst; ++i)
{
for(j = 0; j < csecond; ++j)
{
cout << mult[i][j] << " ";
if(j == csecond - 1)
cout << endl << endl;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.