matrix multiplying

This is a discussion on matrix multiplying within the C++ Programming forums, part of the General Programming Boards category; I have seen all the other threads but I can't get mine to work. I am trying to multiply two ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    27

    matrix multiplying

    I have seen all the other threads but I can't get mine to work. I am trying to multiply two matrices with overloading operators.

    mat[SIZE][SIZE] is my private variable
    SIZE is initalized to 2 for now


    Code:
    Matrix & Matrix::operator *= ( const Matrix & rhs )
    {
      Matrix temp;
      for(int i = 0; r < SIZE; ++i)
      {
        for(int j = 0; j < SIZE; ++j)
        {
          for(int k = 0; k < SIZE; ++k)
          {
            mat[i][j] += mat[i][k] * rhs.mat[k][j];
          }
        }
      }
      return * this;
    }
    
    
    Matrix Matrix::operator *  ( const Matrix & rhs ) const
    {
      Matrix ret_val (* this);
      ret_val *= rhs;
      return ret_val;
    }
    Last edited by noodle24; 04-13-2006 at 10:11 AM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I believe you had it right the first time. I'm pretty sure you need a temporary to do the multiplication. But then once temp holds the result, you must then copy temp to mat before returning *this.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    27
    You're the man. Thanks!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 01:30 PM
  2. multiplying a matrix inputed from a file
    By eater in forum C Programming
    Replies: 16
    Last Post: 04-21-2009, 09:34 AM
  3. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 04:57 PM
  4. Matrix and vector operations on computers
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2004, 06:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21