C++ Operator Overloading. Please help me with the scalar function of this code.
ID: 3830164 • Letter: C
Question
C++ Operator Overloading. Please help me with the scalar function of this code.
Matrix<T> Matrix<T>::operator*(const T &scalar) { Operator Overloading #First-Name Last-Name TODO - Update your name in this readme. TODO - Add a badge from travis CI here ##Problem statement: Overload the operators +,-,* in the class Matrix. Look into the file `Matrix.h` for more details. ## Files to work on * `Matrix.h` Overload the necesseray Operators * You can also modify `main.cpp` to debug your program. * `README.md` to add your name and badge Please **DO NOT MODIFY** any other files. Modifying any other file will result in penalty to your grade. ## Exceptions to be thrown Throw the default exception whereever necessary (Incorrect sizes, etc) ## Matrix Multiplication Order ``` Given the statement: Mat3 = Mat1 * Mat2; Mat1 ={{1, 3, -3}, {-4, 4, 5}, {-1, 2, 0}, {6, 7, 8}}; Mat2 ={{-2, 9, 3, 1}, {10, 4, 5, 11}, {-1, 2, 12, 0}}; Then Mat3 will be: Mat3 :{{31, 15, -18, 34}, {43, -10, 68, 40}, {22, -1, 7, 21}, {50, 98, 149, 83}}; ``` ## Constraints * The class should support the following types via templates: * int * float #include <iostream> #include "Matrix.h" using namespace std; int main() { cout << "Hello, World!" << endl; Matrix<int> m1(2, 2); m1.printMatrix(); Matrix<int> m2(4, 4); m2.printMatrix(); Matrix<int> m3(4, 4); m3.printMatrix(); Matrix<int> m4 = m2 + m3; m4.printMatrix(); //twoD is defined in src/Matrix.h twoD<int> myMatrix = {{2, 2, 2, 2}, {3, 3, 3, 3}, {4, 4, 4, 4}, {5, 5, 5, 5}}; Matrix<int> m5(4, 4, myMatrix); Matrix<int> m6 = m2 + m5; m6.printMatrix(); Matrix<int> m7 = m2 * m3; m7.printMatrix(); #pragma once #include <exception> #include <vector> #include <iostream> #include <vector> using namespace std; template<typename T> using twoD = std::vector<std::vector<T>>; //more info on the above: http://stackoverflow.com/a/16861385/3255842 template<class T> class Matrix { private: int rows; int cols; twoD<T> matrix; protected: void validSizeCheck(int rows, int cols); public: Matrix(int rows, int cols); Matrix(int rows, int cols, twoD<T> newMatrix); twoD<T> getMatrix(); int getRows(); int getCols(); void operator=(const Matrix<T> &); Matrix<T> &operator+=(const Matrix<T> &); Matrix<T> &operator-=(const Matrix<T> &); Matrix<T> &operator*=(const Matrix<T> &); Matrix<T> &operator*=(const T &); /* friend void operator<<(ostream &os, const Matrix<T> &m) { for (int i = 0; i < m.rows; i++) { for (int j = 0; j < m.cols; j++) { os << m.matrix[i][j] << " "; } os << endl; } os << endl; os << endl; } */ void printMatrix(); //T-O-D-O functions are below Matrix<T> operator+(const Matrix<T> &); Matrix<T> operator-(const Matrix<T> &); Matrix<T> operator*(const Matrix<T> &); Matrix<T> operator*(const T &); }; template<class T> void Matrix<T>::validSizeCheck(int rows, int cols) { //DO NOT MODIFY //This is a helper function for checking invalid size. if (rows < 1 || cols < 1) { throw exception(); } } template<class T> twoD<T> Matrix<T>::getMatrix() { //DO NOT MODIFY return matrix; } template<class T> int Matrix<T>::getRows() { //DO NOT MODIFY return rows; } template<class T> int Matrix<T>::getCols() { //DO NOT MODIFY return cols; } template<class T> Matrix<T>::Matrix(int rows, int cols) : rows(rows), cols(cols) { //DO NOT MODIFY validSizeCheck(rows, cols); matrix.resize(rows); for (int i = 0; i < rows; i++) { matrix[i].resize(cols); for (int j = 0; j < cols; j++) { matrix[i][j] = 0; //cout << "Writing: i: " << i << " j: " << j << " val: " << matrix[i][j] << endl; } } } template<class T> Matrix<T>::Matrix(int rows, int cols, twoD<T> newMatrix) : rows(rows), cols(cols) { //DO NOT MODIFY validSizeCheck(rows, cols); matrix.resize(rows); for (int i = 0; i < rows; i++) { matrix[i].resize(cols); for (int j = 0; j < cols; j++) { matrix[i][j] = newMatrix[i][j]; } } } template<class T> void Matrix<T>::operator=(const Matrix<T> &rhs) { //DO NOT MODIFY if (rows != rhs.rows || cols != rhs.cols) { //throw exception } Matrix<T> newMatrix(rows, cols); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { newMatrix.matrix[i][j] = rhs.matrix[i][j]; } } } template<class T> Matrix<T> &Matrix<T>::operator+=(const Matrix<T> &rhs) { //DO NOT MODIFY *this = *this + rhs; return *this; } template<class T> Matrix<T> &Matrix<T>::operator-=(const Matrix<T> &rhs) { //DO NOT MODIFY *this = *this - rhs; return *this; } template<class T> Matrix<T> &Matrix<T>::operator*=(const Matrix<T> &rhs) { //DO NOT MODIFY *this = *this * rhs; return *this; } template<class T> Matrix<T> &Matrix<T>::operator*=(const T &rhs) { //DO NOT MODIFY *this = *this * rhs; return *this; } template<class T> void Matrix<T>::printMatrix() { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { cout << matrix[i][j] << " "; } cout << endl; } cout << endl; cout << endl; } Matrix<T> Matrix<T>::operator*(const T &scalar) { //TODO twoD<int> myMulMatrix1 = { {1, 3, -3}, {-4, 4, 5}, {-1, 2, 0}, {6, 7, 8} }; twoD<int> myMulMatrix2 = { {-2, 9, 3, 1}, {10, 4, 5, 11}, {-1, 2, 12, 0} }; Matrix<int> mulMat1(4, 3, myMulMatrix1); Matrix<int> mulMat2(3, 4, myMulMatrix2); Matrix<int> mulMat3 = mulMat1 * mulMat2; mulMat3.printMatrix(); //cout << mulMat3; return 0;
Explanation / Answer
Here is the code for the scalar function
template<class T>
Matrix<T> Matrix<T>::operator*(const T &scalar) {
//TODO
for(int i = 0; i < rows; ++i)
{
for(int j=0; j<cols; j++)
matrix[i][j] *= scalar;
}
return *this;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.