Reputation: 1
I'm working with C# and I'm trying to multiply a scalar by a matrix and return the results. The problem I have is the arguments can be passed in two ways. They can either come in scalar then the matrix, or the matrix then the scalar, so I need to set up two methods to handle them. I know I can simply duplicate the code in the second method, but as I understand it, a method call is a slicker way to do it since both methods use the same code. I just need some help on what that call would look like. My code is shown below.
public static Matrix operator*(int scalar, Matrix matrix)
{
uint row,col;
Matrix matrixProd;
matrixProd = new Matrix(matrix.Rows,matrix.Cols);
for (row=1; row<=matrixProd.Rows; row++)
for (col=1; col<=matrixProd.Cols; col++)
matrixProd.TwoDArray[row,col] = matrix.TwoDArray[row,col] * scalar;
return matrixProd;
}
public static Matrix operator*(Matrix matrix, int scalar)
{
//I have no idea on what to put here to call the previous method.//
}
Upvotes: 0
Views: 229
Reputation: 7457
You can just reverse the order of the operands like this:
public static Matrix operator*(Matrix matrix, int scalar)
{
return scalar * matrix;
}
Upvotes: 2