``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135`` ``````MATRIXMULT.CPP #include #include #include #include #include "Matrix.h" using namespace std; int SetRows() { int num_rows; cout << "Input the number of rows of the matrix." << endl; cin >> num_rows; return num_rows; } int SetCols() { int num_cols; cout << "Input the number of columns of the matrix." << endl; cin >> num_cols; cout << "\n"; return num_cols; } int SetElements(int num_rows, int num_cols) //Set the number of elements to be the product of the number of rows and columns. { int num_elements = num_rows * num_cols; return num_elements; } void New_Matrix(Matrix M) //Request information for a specific matrix. { cout << "Enter information for "<< M.m_name << ". \n"; } void InstantiateMatrix(Matrix& M) //Input numerical values into our matrix. { cout << "Now input the values held in " << M.m_name << "." <> element; M.m_matrixElements.push_back(element); } cout << endl; } void MatrixMultiplier(Matrix First, Matrix Second, Matrix &Product) // This is where the multiplication occurs. { for( int i = 0; i < First.m_rows ; i++) for (int j = 0 ; j < Second.m_cols ; j ++) for (int k = 0; k < Product.m_cols; k++) Product.m_matrixElements[i*Product.m_cols + j] += First.m_matrixElements[i*First.m_cols+k]*Second.m_matrixElements[j+Second.m_cols*k]; } int main() { cout << "\n\nWelcome to the matrix multiplier!\n" << endl; //Welcome, etc. cout << "You will be asked to input some information" << endl; cout << "about the matrices you wish to multiply and" << endl; cout << "then the computer will make sure the matrice" << endl; cout << "s are multiplicable. Then you will be return" << endl; cout << "ed the product!\n" << endl; Matrix A("Matrix A"), B("Matrix B"), C("Matrix C"); New_Matrix(A); A.m_rows = SetRows(); // Here we initialize some important data members of matrix A. A.m_cols = SetCols(); A.m_elements = SetElements(A.m_rows, A.m_cols); New_Matrix(B); B.m_rows = SetRows(); B.m_cols = SetCols(); B.m_elements = SetElements(B.m_rows, B.m_cols); assert ( A.m_cols == B.m_rows); // Make sure the matrices can be multiplied before anything else. InstantiateMatrix(A); // Here the matrices are filled with numbers. InstantiateMatrix(B); C.m_elements = A.m_cols*B.m_rows; //Set dimensions for matrix C, the product matrix, and reserve some memory for it. C.m_rows = A.m_rows; C.m_cols = B.m_cols; C.m_matrixElements.reserve(C.m_elements); void MatrixMultiplier(A, B, C); // Use the MatrixMultiplier function. for (int i = 0; i < C.m_elements ; i++) //Here we output the product matrix. { cout << C.m_matrixElements[i] << " "; if (( i + 1)% C.m_cols == 0) cout << endl; } } MATRIX.H #ifndef Matrix_Def #define Matrix_Def #include #include using namespace std; class Matrix { public: string m_name; //name the matrix int m_rows; //give it a number of rows, columns, and elements. int m_cols; int m_elements; vector m_matrixElements; // This is the container for the matrix elements Matrix(string name); // This is our constructor. }; #endif MATRIXSTUFF.CPP #include #include "Matrix.h" using namespace std; Matrix::Matrix(string name) : m_name(name) {} // Our constructor. Name the matrix. ``````