I'm a beginner in c++. The other day was trying to multiply 2 matrices using friend function. The program did run ,but the result showed some garbage values. plz help..
In general multiply the row with col then adding the results. if your left matrix has n cols then your right should have n rows.
Here is a simplified version which I wrote some time ago =)
/** Multiplication
Matrix is a class which basically acts like 2 dimensional vector.
**/
Matrix matrix_multiplication_function(Matrix & matrix_1, Matrix & matrix_2){
//Check that rows are of the correct dimension
if(matrix_1.cols() != matrix_2.rows()){
throw std::invalid_argument ("Invalid Dimension");
}
//Create a new matrix with the correct dimension
Matrix result(matrix_1.rows(), matrix_2.cols());
for(int r = 0; r < result.rows(); ++r){
for(int c = 0; c < result.cols(); ++c){
for(int i = 0; i < matrix_1.cols; ++i){
result[r][c] += matrix_1[r][i] * matrix_2[i][c];
}
}
}
//return a copy of result
return result;
}