Thread: multiply 2 matrixes

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    161

    multiply 2 matrixes

    this code should multiply 2 matrixes but it seems it doesn't work how it should.

    output:
    Code:
    Matrix A : 
    0  4  7  
    1  5  5  
    
    Matrix B : 
    0  5  3  6  
    0  7  1  2  
    0  3  2  1  
    
    Matrix C : 
    3  103  50  53  
    0  77  32  35
    the first number of C[1][1] should be 0!!!

    C[1][1] = 0*0+4*0+7*0 = 0 not 3 !! can soneone help me?
    C[1][2] = 0*5+4*7+5*3 = 43 not 103..
    and so on..

    it works sometimes, other times it does not

    source:



    Code:
    #include "stdio.h"
    #include "stdlib.h"
    #include "string.h"
    
    #define RIGAA 2    
    #define COLA  3
    #define RIGAB 3    
    #define COLB  4
    
    int A [RIGAA][COLA];
    int B [RIGAB][COLB];
    int C [RIGAA][COLB];
    
    
    void GenerateMatrixes();
    void GenerateMatrix(int* matrix,int riga,int col);
    void MultiplyaMatrix();
    void PrintMatrix(char* name,int* matrix,int row,int col);
    void CleanScreen();
    
    
    int main(int argc, char* argv[])
    {
    
    	int ch=0;  //scelta dal menu iterattivo
    
    	
    	printf("************************************\n");
    	printf("**     Matrix Multiplication      **\n");
    	printf("************************************\n\n");
    
    
    	GenerateMatrixes();
    	printf("\n\n");
    	
    
    	while(ch!=5) //loop cycle, repeat untill user choose to exit with value 5
    	{   //application menu 
    		ch=0;
    		printf("Regenerate matrixes    : 1 \n");
    		printf("Multiplicate           : 2 \n");
    		printf("Re-print matrixes      : 3 \n");
    		printf("Clean Screen           : 4 \n");
    		printf("Exit                   : 5 \n");
    		printf("Select command: ");
    		scanf("%d",&ch);
    
    		switch(ch)
    		{
    		case 1:
    			GenerateMatrixes();
    			break;
    		case 2:
    			{
    			MultiplyaMatrix();
    			break;
    			}
    		case 3:
    			{
    			PrintMatrix("A",(int*)A,RIGAA,COLA);
    			PrintMatrix("B",(int*)B,RIGAB,COLB);
    			break;
    			}
    		case 4:
    			{
    			//clear the screen
    			CleanScreen();
    			break;
    			}
    		default:
    			CleanScreen();
    			break;
    			
    		};
    	
    	}
    
    	return 0;
    }
    
    
    
    //random generation of matrix values
    void GenerateMatrixes()
    {
    	
    	//generate matrixes A B
    	GenerateMatrix((int*)A,RIGAA,COLA);
    	GenerateMatrix((int*)B,RIGAB,COLB);
    
    	//automatically print of generated matrix
    	printf("\n\n\n");
    	PrintMatrix("A",(int*)A,RIGAA,COLA);
    	PrintMatrix("B",(int*)B,RIGAB,COLB);
    
    	printf("\n\n");
    
    }
    
    
    //Print output of the specified matrix
    void PrintMatrix(char* name,int* matrix,int row,int col)
    {
    	int i,j;
    	char str[20];
    	printf("\n\n");
    
    	sprintf(str,"Matrix %s : ",name);
    	printf(str);
    	printf("\n\n");
    
    	for(i=0;i<row;i++)//ciclo e genero casualmente i valori 
    	{
    		for(j=0;j<col;j++)
    		{
    			sprintf(str,"%d  ",matrix[(i*col)+j]);
    			printf(str);
    		}
    	   printf("\n");
    	}
    
    	printf("\n");
    
    
    }
    
    //Matrix multiplication
    void MultiplyaMatrix()
    {
    	int i; int j; 
    	int m;
        for (i=0; i<RIGAA; i++)
          for (j=0; j<COLB; j++)
    		{
    			for(m=0;m<COLA;m++)
    				C[i][j]+= (A[i][m] * B[m][j]); 
    		}
    
    		PrintMatrix("C",(int*)C,RIGAA,COLB);
    
    }
    
    
    //Randomly generate velues of a matrix given by pointer
    void GenerateMatrix(int* matrix,int riga,int col)
    {
    	int i,j=0;
    
    	for(i=0;i<riga;i++)//random generation of values
    		for(j=0;j<col;j++)
    			matrix[(i*col)+j]=rand()%8;
    }
    
    
    void CleanScreen()
    {
    	system("clear"); // unix system
    }
    This forum is the best one I've ever seen. Great ppl, great coders

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    In your MultiplyaMatrix() function you are not initialising C before summing the individual elements:
    Code:
    void MultiplyaMatrix()
    {
      int i; int j; 
      int m;
    
      for (i=0; i<RIGAA; i++) {
        for (j=0; j<COLB; j++) {
          C[i][j] = 0;   /* <-- initialise before using a loop to sum a number of elements */
          for(m=0;m<COLA;m++) {
            C[i][j]+= (A[i][m] * B[m][j]);
          }
        }
      }
    
      PrintMatrix("C",(int*)C,RIGAA,COLB);
    }
    DavT
    -----------------------------------------------

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    you are soooo right
    thanx

    Jacopo
    This forum is the best one I've ever seen. Great ppl, great coders

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiply link list
    By mradul.exe in forum C Programming
    Replies: 5
    Last Post: 07-12-2008, 07:18 PM
  2. open file with numbers and multiply them
    By autopilot in forum C Programming
    Replies: 30
    Last Post: 09-10-2007, 04:48 PM
  3. multiply by 7
    By agarwaga in forum C Programming
    Replies: 1
    Last Post: 05-22-2006, 02:19 PM
  4. Very handy matrix functions - part 1
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 05-20-2004, 10:38 AM
  5. matrixes for collision detection
    By DavidP in forum Game Programming
    Replies: 10
    Last Post: 11-09-2002, 10:31 PM