Thread: Matrix Multiplication

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    24

    Matrix Multiplication

    Code:
    #include <stdio.h>
    #define M 2
    #define N 3
    #define K 3
    
    // A[M][N]*B[N][K] = C[M][K]
    void main()
    {
    	int A[M][N]={{1,2,3},{4,5,6}} , B[N][K]={{1,1,1,},{2,2,2,},{1,1,1}} , C[M][K]={0};
    	int i, j ;
    
    	for(i=0; i<M; i++)
    		for(j=0; j<K; j++)
    			for(j=C[i][j]=0; j<N; j++)
    				C[i][j] += A[i][j]*B[i][j];
    
    	for(i=0; i<M; i++)
    	{
    		for(j=0; j<K; j++)
    			printf("%3d",C[i][j]);
    		printf("\n");
    	}
    }
    why the answer not correct ?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The two inner loops of your multiplication are using the same controlling variable (j). There is probably another variable (named k to continue with your naming convention) needed to control the innermost loop.

    Oh, and main() returns int, not void. Anyone or any documentation that told you main() returns void is deficient.
    Last edited by grumpy; 01-21-2010 at 04:56 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105
    TRy out like this for multiplying the matrices
    Code:
     printf("\n\nTHE EQUIVALENT MULTIPLICATION MATRIX IS:\n\n");
     for(i=0;i<M;i++)
        {
         for(j=0;j<K;j++)
    	{
    	 sum=0;
    	 for(n=0;n<N;n++)
    	    {
    	     sum = sum + (mat1[i][n] * mat2[n][j]);
    	    C[i][j] = sum;
    	    }
    
    	 printf("%-5d ",C[i][j]);
    	 if(j==c2-1)
    	  {
    	   printf("\n");
    	  }
    	}
        }
    This will work out.......

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 01:30 PM
  2. *operator overloading: scalar matrix multiplication
    By gemini_shooter in forum C++ Programming
    Replies: 4
    Last Post: 06-08-2009, 01:14 PM
  3. 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
  4. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 05:57 PM