Matrix Multiplication with simple syntax! plz help.

• 11-04-2004
codebox
Matrix Multiplication with simple syntax! plz help.
hi all..i hope every1 is fine... my teacher gav me this assignment and i m stuck... i cant seem to get the right answer..i hav spent hours but no use :( plz see the code...

Its a simple matrix multiplication program. There are 3 matrices in the program m1,m2,m3...m1 and m2 are multiplied and m3 is assignmed the result...

m1=| 2 1 6 |
| 5 4 0 |

m2=| 5 1 |
| 9 7 |
| 6 2 |

The result has to be:

m3=| 55 21 |
| 61 33 |

but i m getting something else! i m getting: 55 55,33 29

m3=| 55 55 |
| 33 29 |

Here is the code:

Code:

```#include <stdio.h> #include <conio.h> void main(void) {         int m1[2][3], m2[3][2], m3[2][2], i, j, k;         clrscr();         printf("First Matrix\n");         printf("============\n");         // populate the first matrix         for (i=0; i<2; i++) {                 for (j=0; j<3; j++) {                         printf("Enter value for position [%d][%d]: ",i,j);                         scanf("%d", &m1[i][j]);                 }         }         printf("\nSecond Matrix\n");         printf("=================\n");         // populate the second matrix         for (i=0; i<3; i++) {                 for (j=0; j<2; j++) {                         printf("Enter value for position [%d][%d]:",i,j);                         scanf("%d",&m2[i][j]);                 }         }         printf("\nMatrix Multiplication\n");         printf("=====================\n");         for (i=0; i<2; i++) {                 for (j=0; j<2; j++) {                         for (k=0; k<2; k++) {                                 m3[i][j] = (m1[i][j]*m2[j][i])+(m1[i][j+1]*m2[j+1][i])+(m1[i][j+2]*m2[j+2][i]);                         }                 }         }         // print the m3 matrix         for (i=0; i<2; i++) {                 for (j=0; j<2; j++) {                         printf("\nValue at position [%d][%d] is: %d",i,j,m3[i][j]);                 }         }         getch(); }```
Any help is highly appreciated... thanks in advance...
regards,
Kamran.
• 11-04-2004
Dave_Sinkula
Be careful with array bounds.
Code:

`int m1[2][3], m2[3][2], m3[2][2], i, j, k;`
Code:

```  for ( i=0; i<2; i++ ) {       for ( j=0; j<2; j++ ) {         for ( k=0; k<2; k++ ) {             m3[i][j] = (m1[i][j]*m2[j][i])+(m1[i][j+1]*m2[j+1][i])+(m1[i][j+2]*m2[j+2][i]);```
If j=1, then j+2=3, and m1[i][3] and m2[3][i] are outside of their arrays.
• 11-04-2004
codebox
thanks for reply Dave... yeh i can understand that... but thats what i am asking.. that what shud i change in the code so it becomes correct?
• 11-04-2004
quzah
Quote:

what shud i change in the code so it becomes correct?
Well you could start by fixing main. If you don't know how, you would be best served to read this FAQ entry.

Other than that, simply rethink your loop. You'll need to adjust your numbers so that when your loop plays out you don't run out of bounds.

Quzah.
• 11-04-2004
Dave_Sinkula
>what shud i change in the code so it becomes correct?

What are you using k to do? I would imagine you want an innermost loop like this.
Code:

```        m3[i][j] = 0;         for ( k = 0; k < 3; ++k )         {             m3[i][j] += m1[i][k] * m2[k][j];         }```
• 11-05-2004
codebox
quzah, thanks for telling me the FAQ entry... i really didnt know that ! i'll be careful next time...

dave, thanks man but thats not doing the job... :) i m still on it... and if i cant do it by tonite, then i'll ask any of the senior students at my UNI... coz exams are not far so i hav to practice it as well...

thanks again... c ya soon :)
• 11-05-2004
VirtualAce
Code:

```for (i=0; i<2; i++) {                 for (j=0; j<2; j++) {                         for (k=0; k<2; k++) {                                 m3[i][j] = (m1[i][j]*m2[j][i])+(m1[i][j+1]*m2[j+1][i])+(m1[i][j+2]*m2[j+2][i]);                         }                 }```
This code is for a 3x3. You only need to change it a bit to support 3x3 mul 2x3
Code:

```for (int i=0;i<2;i++) { for (int j=0;j<2;j++) { m3[i][j]=0.0f; for (int k=0;k<2;k++) { m3[i][j]+=m1[i][k]*m2[k][j]; } } }```
However the inner loop is not unrolled and is deathly slow. Also I don't recommend using matrices this way.

A better way is to create a set of structs.
Code:

```struct M3x2 { float _00,_01,_02; float _10,_11,_12; }   struct M2x3 { float _00,_01; float _10,_11; float _20,_21; };   struct M3x3 { float _00,_01,_02; float _10,_11,_12; float _20,_21,_22; }   struct M4x4 { float _00,_01,_02,_03; float _10,_11,_12,_13; float _20,_21,_22,_23; float _30,_31,_32,_33; };```
Then to multiply you don't need a loop.

Rememeber this:

A*B=C

C(ij)=A(i)*B(j)

Columns of A must equal rows of B to be multiplied together.

The ij entry of C is equal to the dot product of ith row of A with the jth column of B.

A * B =
[A1 dot B1, A1 dot B2]
[A2 dot B1, A2 dot B2]