Multiplying 2 2-dimensional matrices

This is a discussion on Multiplying 2 2-dimensional matrices within the C Programming forums, part of the General Programming Boards category; Hello, I'm trying to write a simple program which multiplies 2 2-dimensional matrices. I get no compiler errors nor warnings, ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    69

    Multiplying 2 2-dimensional matrices

    Hello,

    I'm trying to write a simple program which multiplies 2 2-dimensional matrices. I get no compiler errors nor warnings, also with debugging my pointer values seem to be okay, but the problem is that it prints the adresses, anyone got an idea?
    This is my code:
    Code:
    /* AUTH: Kevin Strijbos   DATE: 18/01/2012
       DESCR: multiplies 2 2-dimensional matrices */
    
    
    #include <stdio.h>
    
    
    void mul_Matrices (int *m1, int *m2, int *result);
    
    
    int main (void)
    {
    	int m1[2][2] = {{1, 2},{1, 3}};
    	int m2[2][2] = {{3, 5},{4, 2}};
    	int result[2][2];
    	int counter, smallCounter, index;
    
    
    	index = 0;
    
    
    	mul_Matrices(m1, m2, result);
    
    
    	printf("Result:\n");
    
    
    	for (counter = 0; counter < 2 ;counter++)
    	{
    		for (smallCounter = 0; smallCounter < 2 ;smallCounter++)
    		{
    			printf("%d\t", result[index]);
    			index++;
    		}
    		printf("\n");
    	}
    }
    
    
    /* function that multiplies 2 2-dimensional arrays */
    void mul_Matrices (int *m1, int *m2, int *result)
    {
    	int counter, smallCounter;
    
    
    	for (counter = 0; counter < 2 ;counter++)
    	{
    		for (smallCounter = 0; smallCounter < 2 ;smallCounter++)
    		{
    			*result++ = (*m1 * *m2++) + (*(m1+1) * *m2++); // formula for matrix multiplation
    		}
    		m1 += 2;
    		m2 -= 4;
    
    
    	}
    }
    And this is my output:

    Result:
    3800320 3800328
    3800336 3800344
    Press any key to continue . . .

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,699
    I don't know why you don't get any errors, but here are the errors my compiler produced:

    main.c||In function ‘main’:|
    main.c|22|error: passing argument 1 of ‘mul_Matrices’ from incompatible pointer type|
    main.c|8|note: expected ‘int *’ but argument is of type ‘int (*)[2]’|
    main.c|22|error: passing argument 2 of ‘mul_Matrices’ from incompatible pointer type|
    main.c|8|note: expected ‘int *’ but argument is of type ‘int (*)[2]’|
    main.c|22|error: passing argument 3 of ‘mul_Matrices’ from incompatible pointer type|
    main.c|8|note: expected ‘int *’ but argument is of type ‘int (*)[2]’|
    main.c|32|warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’|
    main.c||In function ‘mul_Matrices’:|
    main.c|50|warning: operation on ‘m2’ may be undefined|
    ||=== Build finished: 6 errors, 2 warnings ===|
    Jim

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    Well, that's what happened yesterday also. I don't get any warnings nor errors, but others do?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,699
    What compiler are you trying to use?

    Jim

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    The compiler in microsoft visual studio, think it's GCC?

  6. #6
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    If you're going to use result like it's a one-dimensional array, then define it as such. Otherwise you should access the array like it's actually two dimensional: with a row and column index.
    Consider this post signed

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    2,699
    The compiler in microsoft visual studio, think it's GCC?
    No that would be Microsoft Visual C.

    Are you compiling through the IDE or trying to use the command line?

    Jim

  8. #8
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    bernt: doesn't matter, arrays are row-oriented in the memory. So I can use my result array like this, don't need to give a row and column inde.

    jim: I'm compiling through the IDE

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    doesn't matter, arrays are row-oriented in the memory. So I can use my result array like this, don't need to give a row and column inde
    And yet it doesn't quite work, does it.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    2,699
    You need to check your compiler settings. You need to insure warnings are being generated, and that you have selected the highest level (w4). Your program has quite a few errors that your compiler should be telling you about.
    The following line:
    Code:
                *result++ = (*m1 * *m2++) + (*(m1+1) * *m2++);
    Causes undefined behavior, you can not increment a variable twice in the same sequence point (m2).

    As others have said, you have multidimensional arrays, use array notation[][] to access the elements.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiplying two matrices
    By Vin in forum C Programming
    Replies: 1
    Last Post: 03-01-2010, 03:54 AM
  2. Overflow multiplying matrices
    By Logicbynumbers in forum C Programming
    Replies: 11
    Last Post: 11-17-2008, 05:53 PM
  3. multiplying matrices
    By RenderedAwake in forum C++ Programming
    Replies: 2
    Last Post: 03-22-2005, 10:36 PM
  4. Multiplying matrices
    By Star Lancer in forum C++ Programming
    Replies: 7
    Last Post: 05-22-2003, 06:07 AM
  5. Problem multiplying rotation matrices together
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 03-04-2003, 08:20 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21