Hi, I have to build a matrix class with functions that you would expect, Im wondering if anyone can tell me why one or both of these functions arent working correctly? Basically a test harness is run against them to check so the data is random. Im sure it is most likely something simple but I thought I had it correct. Thanks in advance for anyone who takes the time to look this over. :)

.h

#ifndef __MATRIX_H__
#define __MATRIX_H__

class CMatrix
{
public:
	float m_fMatrix[4][4];
};

#endif // __MATRIX_H_

Set identity matrix.

void 
CExercises::SetIdentity(CMatrix& _rResult)
{
    // Implement your logic here:
	for(int i = 0; i < 4; ++i)
	{
		for(int j = 0; j < 4; ++j)
		{
			_rResult.m_fMatrix[i][j] = 0;

			if((i == 1) && (j == 1))
			{
				_rResult.m_fMatrix[i][j] = 1;
			}
			
			if((i == 2) && (j == 2))
			{
				_rResult.m_fMatrix[i][j] = 1;
			}

			if((i == 3) && (j == 3))
			{
				_rResult.m_fMatrix[i][j] = 1;
			}

			if((i == 4) && (j == 4))
			{
				_rResult.m_fMatrix[i][j] = 1;
			}
		}

	}
}

multiply matrix function.

void 
CExercises::Multiply(const CMatrix& _rLHS, const CMatrix& _rRHS, CMatrix& _rResult)
{
    // Implement your logic here:
	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 4; j++) 
		{ 
			float temp = 0;

			for (int k = 0; k < 4; k++) 
			{	
				temp += _rRHS.m_fMatrix[i][k] * _rLHS.m_fMatrix[k][j];
			}
			_rResult.m_fMatrix[i][j] = temp;
		}
	} 

}

What exactly is the problem? The only thing that I can see that might be a problem is that the LHS and RHS matrices might have their indices the wrong way araoud (I think that this is a mistake I made the other day too when I was answering a very similar question). Anyway, I think that you line 13 should probably read:

temp += _LHS.m_fMatrix[i][k] * _RHS.m_fMatrix[k][j];

Also, you SetIdentity() function is very inefficient. In general it's a bad idea to have lots of if statements inside lots of nested for loops if you can avoid it. I would go fo something more like:

void CExercises::SetIdentity(CMatrix& _rResult){

   /* Set everything to zero */
   for(int i = 0; i < 4; ++i){
      for(int j = 0; j < 4; ++j)
         _rResult.m_fMatrix[i][j] = 0;
   }

   /* Now set the diagonals to 1 */
   for ( int i = 0; i < 4; ++i )
      _rResult.m_fMatrix[i][i] = 1;
}

If you're using STL algorithms, you can make this function look a bit more streamline by using the std::fill function:

void CExercises::SetIdentity(CMatrix& _rResult){

   /* Set everything to zero */
   std::fill( _rResult.m_fMatrix, _rResult.m_fMatrix + 16, 0 );

   /* Now set the diagonals to 1 */
   for ( int i = 0; i < 4; ++i )
      _rResult.m_fMatrix[i][i] = 1;
}

Thanks again Ravenous, I think it may have been the SetIdentity() function that was acting up. Anyway i've got the whole thing working now thanks to you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.