Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Operator Overloading. Please help me with the scalar function of this code. Matrix Matrix :: operator *( const T &scalar) { Operator Overloading #First-Name

C++ Operator Overloading. Please help me with the scalar function of this code. 
Matrix Matrix::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  #include "Matrix.h" using namespace std; int main() { cout << "Hello, World!" << endl; Matrix m1(2, 2); m1.printMatrix(); Matrix m2(4, 4); m2.printMatrix(); Matrix m3(4, 4); m3.printMatrix(); Matrix m4 = m2 + m3; m4.printMatrix(); //twoD is defined in src/Matrix.h twoD myMatrix = {{2, 2, 2, 2}, {3, 3, 3, 3}, {4, 4, 4, 4}, {5, 5, 5, 5}}; Matrix m5(4, 4, myMatrix); Matrix m6 = m2 + m5; m6.printMatrix(); Matrix m7 = m2 * m3; m7.printMatrix(); 
 #pragma once #include  #include  #include  #include  using namespace std; template using twoD = std::vector>; //more info on the above: http://stackoverflow.com/a/16861385/3255842 template class Matrix { private: int rows; int cols; twoD matrix; protected: void validSizeCheck(int rows, int cols); public: Matrix(int rows, int cols); Matrix(int rows, int cols, twoD newMatrix); twoD getMatrix(); int getRows(); int getCols(); void operator=(const Matrix &); Matrix &operator+=(const Matrix &); Matrix &operator-=(const Matrix &); Matrix &operator*=(const Matrix &); Matrix &operator*=(const T &); /* friend void operator<<(ostream &os, const Matrix &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 operator+(const Matrix &); Matrix operator-(const Matrix &); Matrix operator*(const Matrix &); Matrix operator*(const T &); }; template void Matrix::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 twoD Matrix::getMatrix() { //DO NOT MODIFY return matrix; } template int Matrix::getRows() { //DO NOT MODIFY return rows; } template int Matrix::getCols() { //DO NOT MODIFY return cols; } template Matrix::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 Matrix::Matrix(int rows, int cols, twoD 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 void Matrix::operator=(const Matrix &rhs) { //DO NOT MODIFY if (rows != rhs.rows || cols != rhs.cols) { //throw exception } Matrix 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 Matrix &Matrix::operator+=(const Matrix &rhs) { //DO NOT MODIFY *this = *this + rhs; return *this; } template Matrix &Matrix::operator-=(const Matrix &rhs) { //DO NOT MODIFY *this = *this - rhs; return *this; } template Matrix &Matrix::operator*=(const Matrix &rhs) { //DO NOT MODIFY *this = *this * rhs; return *this; } template Matrix &Matrix::operator*=(const T &rhs) { //DO NOT MODIFY *this = *this * rhs; return *this; } template void Matrix::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 Matrix::operator*(const T &scalar) { //TODO 
 twoD myMulMatrix1 = { {1, 3, -3}, {-4, 4, 5}, {-1, 2, 0}, {6, 7, 8} }; twoD myMulMatrix2 = { {-2, 9, 3, 1}, {10, 4, 5, 11}, {-1, 2, 12, 0} }; Matrix mulMat1(4, 3, myMulMatrix1); Matrix mulMat2(3, 4, myMulMatrix2); Matrix mulMat3 = mulMat1 * mulMat2; mulMat3.printMatrix(); //cout << mulMat3; return 0; 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Mining Concepts And Techniques

Authors: Jiawei Han, Micheline Kamber, Jian Pei

3rd Edition

0123814790, 9780123814791

More Books

Students also viewed these Databases questions