Thread: Matrix multiplication

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    20

    Matrix multiplication

    So I have my program up and running but it us outputting the wrong values. 2 and of the four values are wrong. The first and last values are wrong for some reason while the second and third values are correct. Thanks everyone.

    Code:
    #include <stdio.h>
    #include "conio.h"
    int main()
    { // 4x1
    	int  product, entries, current, input;
    	char buffer[1028];
    	int a,b,i,j;
    	int x[4] [4] = {{ 0,1,1,0}, {1,0,0,0}, {0,1,0,0}, {0,1,0,1}}; //i has 4 numbers j has 4 numbers
    	int matrix [4][1];
    	
    	
    	
    	for (a=0; a <4; a++)
    	{
    		for(b=0; b< 1; b++)
    		{
    			printf("Please enter in four entries for a 4*1 matrix.");
    			scanf("%d", &matrix[a][b]);
    		}
    	}
    	
    	
    	for (i = 0; i < b; i++){
    		
    
    		for ( j =0; j < a ; j++) 
    		{
    	product =x[i][j] * matrix[i][j]; 
    	printf("The product of the two matricies is %d\n", product);
    	_getch();	}
    	}
    	
    
    	getchar();
    	return 0;
    	
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    I don't think matrix multiplication is commutative. Given matrices A and B the product AB will have as many rows as A and as many columns as B. You multiplied x by matrix, so there is no right matrix.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Thanks for what? You didn't ask us anything.

    You have a buffer overrun, and no that's not how you perform matrix multiplication. 'x' and 'matrix' aren't the same size, so what makes you think you can use the same values to index into both of them?
    Go and look up how to perform matrix multiplication. You have gotten the algorithm wrong.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Your second set of loops is not performing matrix multiplication at all. It is simply printing out the product of elements with the same index in x and matrix.

    There is also the problem that the loops reading matrix work over a and b, and then you use the last values of a and b to control number of iterations in the loops doing your "product". Such tricks increase chances of getting things wrong. Buffer overruns when reading matrix won't help much.
    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.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    20
    Ok, this is what I have now. I'm not sure where I am going wrong here Can someone please point me in the right direction?

    Code:
    #include <stdio.h>
    #include "conio.h"
    int main()
    { // 4x1
    	int entries, current, input;
    	int product[4];
    	int a,b,row,col;
    	int x[4] [4] = {{ 0,1,1,0}, {1,0,0,0}, {0,1,0,0}, {0,1,0,1}}; //i has 4 numbers j has 4 numbers
    	int matrix [4];
    	
    	
    	
    	for (a=0; a <4; a++)
    	{
    		for(b=0; b< 1; b++)
    		{
    			printf("Please enter in four entries for a 4*1 matrix.");
    			scanf("%d", &matrix[b]);
    		}
    	}
    	
    	
    	for (row = 0; row < 4; row++){
    		product[row] = 0;
    
    		for ( col =0; col < 4 ; col++) 
    		{ 
    			product[row] =  product[row] + x[row][col]*matrix[col]; 
    				
    		} 
    		printf("Row %d of The product of the two matricies is %d\n", row, product[row]);
    	_getch();
    	}
    	
    
    	getchar();
    	return 0;
    	
    }

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    		for(b=0; b< 1; b++)
    		{
    			printf("Please enter in four entries for a 4*1 matrix.");
    			scanf("%d", &matrix[b]);
    		}
    Look closely... how often will that loop execute? How many elements are in your matrix?

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    20
    Thank you Thank you. The one part of the program I wasn't looking at. You guys are awesome! CommonTater you are in fact an ExtroidinaryTater!

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Just wait till you get my bill....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 05-14-2011, 09:28 AM
  2. Matrix Multiplication in C
    By lombardom in forum C Programming
    Replies: 11
    Last Post: 05-14-2010, 06:57 AM
  3. Matrix Multiplication
    By dlf in forum C++ Programming
    Replies: 18
    Last Post: 12-07-2005, 07:26 AM
  4. matrix multiplication
    By fyodor in forum C Programming
    Replies: 2
    Last Post: 05-27-2002, 07:28 PM
  5. Matrix Multiplication
    By Warped1 in forum C++ Programming
    Replies: 2
    Last Post: 11-05-2001, 11:48 PM