0

Can someone help me with this problem...
A is an m*n matrix
B is an n*p matrix
product AB is an m*p
then I have to write a function void matrix product that takes two matrices of integers as two separate arguments and then calculates the product of those matrices and prints the results on screen. Create constants to store sizes of matrices.

3
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by tintin.iitk
0

First you should represent the matrix:

int **matrix_1=new int*[m];
            for(i=0; i<m; i++){ matrix_1[i] = new int[n];}
// then assign the value to each element.
// so a matrix_1 is formed and its demission is m*n;
// the same can applied to matrix_2, which is n*p;
// matrix_3 which is m*p;
// as the calculation of the product. maybe can handle like this:
        for(i=0, i<m; i++ )
            for(j=0; j<p: j++)
                  matrix_3[i][j] = matrix_1[][]*matrix_2[][];
//this is the key.
0
template <typename T, int m, int n, int p>
Matrix<T, m, p> operator *(const Matrix<T, m, n>& A, const Matrix<T, n, p>& B) {
	Matrix<T, m, p> ret;
	for (int i = 0; i < m; i++)
		for (int j = 0; j < p; j++) {
			ret[i][j] = 0;
			for (int k = 0; k < n; k++)
				ret[i][j] += (A[i][k] * B[k][j]);
		}
	return ret;
}

You can accordingly replace the matrix template instances with properly allocated double or single arrays.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.