# Matrices multiplicarion algorithm

• 04-03-2007
Hussain Hani
Matrices multiplicarion algorithm
Hi folks, i'm doing a program that recieves two matrices from the user and multiply them together. All my code is done except for the multiplication function. I know that i must use "for loops" but how many :S

i'm passint object to be the operand while the the calling object is having the "this" pointer, it goes like this

let A be the first matrice
let B be the second matrice

A.operator*(B);

i have absolutely no idea, this is the function:
Code:

```Matrix Matrix::operator*(const Matrix& rhs) {       Matrix result;             //is this correct ?       result = *(this)*rhs;             if (this == &rhs)       {                 return *this;       }       else       {             //Checks if matrices are able to be multiplied             if (this->m_Columns == rhs.m_Rows)             {                                 //Do multiplication                              }             else             {                 cout << "Cannot perform multiplication." << endl;                 cout << "Columns of first matrix don't ";                 cout << "match the rows of the second matrix" << endl;             }       }             return result; }```
ca any one help please :)
• 04-03-2007
Salem
Well have you looked up how to do matrix multiplication on paper?
Say
http://mathworld.wolfram.com/MatrixMultiplication.html
• 04-03-2007
Oysterman
Code:

```      //is this correct ?       result = *(this)*rhs;```
I wouldn't believe so? That just calls the operator* method recursively, right? Over and over again...

The simplest algorithm for matrix multiplication is O(n^3), and as such would require three for-loops. Saying anything more than that would probably give away the solution (I suspect this is homework). If you can do it on paper, you should be able to translate it to a computer program with a little effort.
• 04-03-2007
robatino
There are more complicated algorithms, though, which are asymptotically more efficient than O(n^3) - see

http://en.wikipedia.org/wiki/Matrix_multiplication

(though implementing them is way beyond a homework assignment).
• 04-03-2007
Hussain Hani
it's not any homework :) i'm just doing that to improve my operator overloading skills + improving my dynamic memory use :)

also i've taken vow that i will never take C++ to the next level (API + DirectX) until i make decent dos math program :D