Thread: Matrix Multiplication with simple syntax! plz help.

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    3

    Unhappy 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.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    3
    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?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >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];
             }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Nov 2004
    Posts
    3
    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

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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]
    Last edited by VirtualAce; 11-05-2004 at 03:03 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. *operator overloading: scalar matrix multiplication
    By gemini_shooter in forum C++ Programming
    Replies: 4
    Last Post: 06-08-2009, 01:14 PM
  2. Matrix Multiplication ( Accessing data from a file ) HELP
    By six_degreez in forum C Programming
    Replies: 2
    Last Post: 07-24-2008, 05:21 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. matrix multiplication using MPI C
    By cromologic in forum C Programming
    Replies: 0
    Last Post: 04-29-2008, 09:35 AM
  5. Very handy matrix functions - part 1
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 05-20-2004, 10:38 AM