Matrix Product

closed account (1TpXSL3A)
Hi guys. I'm trying to calculate the Matrix product which is the sum of the products of elements from both matrices.
When i use the code below i get the elements of the new matrix in list form but surely if i'm finding the matrix product i need the sum of all these product elements.
I'm not sure how to get it so that all my numbers add up and give me a final value for C

I might be getting a bit confused here so any help would be really great!
Thanks guys


1
2
3
4
5
6
7
8
9
10
  {
	for ( int i = 0; i < 3; i++ )
      for ( int j = 0; j < 3; j++ )
	  { 
		 C =+ A[i][j]*B[i][j];

		 cout << C << endl;

	  }
		 
Hi,

Try moving line 7 out side the loops, at the moment the answer will be the last number in your output.

Also the operator on line 5 is += not =+

Try to avoid magic numbers like 3 in your code, make them const variables:

1
2
3
4
5
6
7
8
9
const unsigned short SizeA = 3;
const unsigned short SizeB = 3;

	for ( int i = 0; i < SizeA; i++ ) {
              for ( int j = 0; j < SizeB; j++ ) {


              }
       }


Variable names like i and j can be error prone because they look so similar, try making them words instead.
Topic archived. No new replies allowed.