### Help with Matrix Multiplication

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320`` ``````#include #include using namespace std; class Row { double *arrArray; int nLength; public: Row() { } Row(int length) { this->nLength = length; this->arrArray = new double[length]; for (int i = 0; i < length; i++) { cout << "Enter your number: " << endl; cin >> arrArray[i]; } } Row(int length, double* arr) { this->nLength = length; this->arrArray = arr; } // Get int GetLength () { return (this->nLength); } double* GetArray() { return (this->arrArray); } // Set void SetLength(int length) { this->nLength = length; } void SetArray(double* arr) { this->arrArray = arr; } const double operator[](int Index) const { return (this->arrArray)[Index]; } const Row operator+(const Row rRow) const { for (int i = 0; i < this->nLength; i++) { (this->arrArray)[i] += rRow[i]; } return Row(rRow.nLength, this->arrArray); } const Row operator-(const Row rRow) const { for (int i = 0; i < this->nLength; i++) { (this->arrArray)[i] -= rRow[i]; } return Row(rRow.nLength, this->arrArray); } const Row operator*(const Row rRow) const { for (int i = 0; i < this->nLength; i++) { (this->arrArray)[i] *= rRow[i]; } return Row(rRow.nLength, this->arrArray); } const bool operator==(const Row rRow) const { bool bAreEqual = true; for (int i = 0; i < this->nLength; i++) { if (rRow[i] != (this->arrArray)[i]) { bAreEqual = false; } } return (bAreEqual); } const Row operator=(Row rRow) const { for (int i = 0; i < this->nLength; i++) { this->arrArray[i] = rRow.GetArray()[i]; } return Row(rRow.nLength, this->arrArray); } void operator++() const { for (int i = 0; i < this->nLength; i++) { (this->arrArray)[i] += 1; } } void operator--() const { for (int i = 0; i < this->nLength; i++) { (this->arrArray)[i] -= 1; } } void operator!() const { for (int i = 0; i < this->nLength; i++) { (this->arrArray)[i] *= (-1); } } void PrintRow() { for(int i=0;i < this->nLength; i++) { cout << this->arrArray[i] << ", "; } cout << endl; } }; class Matrix { int nCols; Row *mMatrix; public: Matrix() { } Matrix(int cols, Row* matrix) { this->nCols = cols; this->mMatrix = matrix; } // Get int GetLength () { return (this->nCols); } Row* GetArray() { return (this->mMatrix); } // Set void SetLength(int length) { this->nCols = length; } void SetArray(Row* matrix) { this->mMatrix = matrix; } const Row operator[](int Index) const { return (this->mMatrix)[Index]; } const Matrix operator+(const Matrix mMat) const { if (this->nCols != mMat.nCols) { cout << "לא ניתן לבצע את הפעולה" << endl; } else { for (int i = 0; i < this->nCols; i++) { for (int j = 0; j < this->mMatrix[0].GetLength(); j++) { (this->mMatrix[i]).GetArray()[j] += mMat[i][j]; } } } return Matrix(this->nCols, this->mMatrix); } //Matrix operator*(Matrix mMat) //{ // // if ((this->mMatrix[1].GetLength() == mMat.nCols)) // { // Matrix NewMatrix((this->mMatrix)->GetLength(), mMat.GetArray()->GetLength()); // for (int i = 0; i < mMat.nCols; i++) // { // for (int j = 0; j< mMat.nCols; j++) // { // for (int inner = 0; inner < this->GetLength(); inner++) // { // //NewMatrix.GetArray()[i].GetArray()[j] = (this[i][inner] * mMat[inner][j]); // } // } // } // } // else if ((mMat.GetArray()[0].GetLength())) // { // } // else // { // cout << "לא ניתן לבצע את הפעולה" << endl; // } // return Matrix(this->nCols, this->mMatrix); //} /* void MultilpyMatrix() { for (int i = 0; i < this->nCols; i++) { for (int j = 0; j < this->mMatrix[0].GetLength(); j++) { //this->mMatrix[i].GetArray()[j]=mMat[i][j] this->mMatrix[i][j]= (matrix1[j]*matrix2[j])+(matrix1[j+1]*matrix2[j+1])+(matrix1[j+2]*matrix2[j+2]) } } } */ void PrintMatrix() { for(int i=0;i < this->nCols; i++) { for(int j=0;j < this->mMatrix[0].GetLength(); j++) { cout << this->GetArray()[i][j] << ", "; } cout << endl; } cout << endl; } }; void main() { /* Row a(2); Row b(2); Row* maarah = new Row[2]; maarah[0] = a; maarah[1] = b; Matrix mat(2, maarah); */ //Checking: Row Row1(5); Row Row2(5); Row1=Row1+Row2; cout << "Sum Rows: \n"; Row1.PrintRow(); Row1=Row1-Row2; cout << "Substract Rows: \n"; Row1.PrintRow(); Row1=Row1*Row2; cout << "Multiply Rows: \n"; Row1.PrintRow(); Row1++; cout << "Operator ++: \n" ; Row1.PrintRow(); Row1--; cout << "Operator --: \n"; Row1.PrintRow(); !Row1; cout << "Operator !: \n"; Row1.PrintRow(); Row* maarah = new Row[2]; maarah[0] = Row1; maarah[1] = Row2; cout << endl << endl; //Matrix Mat2(2, 5); //Matrix Mat1(2, maarah); //Mat1.PrintMatrix(); //Mat1*Mat2; //cout << mat[1][1] << endl; };``````

I need to override * in Matrix class and do a matrix multiplication.
Any suggeststions?

Thank!
to overwrite, use "&" in the function definition.
`void my_function(int &my_variable);`
for the matrix multiplication check wikipedia.
But it works without "&"...
guess you are using global variables then. that also works
Topic archived. No new replies allowed.