Multiplication of 2 Matrix in C

Discussion in 'C' started by JamesBond, Oct 15, 2006.

  1. JamesBond

    JamesBond New Member

    Joined:
    Oct 15, 2006
    Messages:
    3
    Likes Received:
    1
    Trophy Points:
    0

    Background



    The output to the Matrix multiplication will be generated as follows.
    Code:
    a11 a12 a13   A11 A12 A13   a11xA11+a12xA21+a13xA31 a11xA12+a12xA22+a13xA32
    a21 a22 a23 x A21 A22 A23 = a21xA11+a22xA21+a23xA31 a21xA12+a22xA22+a23xA32
    a31 a32 a33   A31 A32 A33   a31xA11+a32xA21+a33xA31       
    
    More specific details are beyond the scope of this article but you can refer Matrix multiplication in wikipedia.

    The Code



    Code:
    void main()
    {
    	int m1[10][10],i,j,k,m2[10][10],add[10][10],mult[10][10],r1,c1,r2,c2;
    	printf("Enter number of rows and columns of first matrix MAX 10\n");
    	scanf("%d%d",&r1,&c1);
    	printf("Enter number of rows and columns of second matrix MAX 10\n");
    	scanf("%d%d",&r2,&c2);
    	if(r2==c1)
    	{
    		printf("Enter rows and columns of First matrix \n");
    		printf("Row wise\n");
    		for(i=0;i<r1;i++)
    		{
    			for(j=0;j<c1;j++)
    				scanf("%d",&m1[i][j]);
    		}
    		printf("You have entered the first matrix as follows:\n");
    		for(i=0;i<r1;i++)
    		{
    			for(j=0;j<c1;j++)
    				printf("%d\t",m1[i][j]);
    			printf("\n");
    		}
    		printf("Enter rows and columns of Second matrix \n");
    		printf("Again row wise\n");
    		for(i=0;i<r2;i++)
    		{
    			for(j=0;j<c2;j++)
    				scanf("%d",&m2[i][j]);
    		}
    		printf("You have entered the second matrix as follows:\n");
    		for(i=0;i<r2;i++)
    		{
    			for(j=0;j<c2;j++)
    				printf("%d\t",m2[i][j]);
    			printf("\n");
    		}
    		if(r1==r2&&c1==c2)
    		{
    			printf("Now we add both the above matrix \n");
    			printf("The result of the addition is as follows;\n");
    			for(i=0;i<r1;i++)
    			{
    				for(j=0;j<c1;j++)
    				{
    					add[i][j]=m1[i][j]+m2[i][j];
    					printf("%d\t",add[i][j]);
    				}
    				printf("\n");
    			}
    		}
    		else
    		{
    			printf("Addition cannot be done as rows or columns are not equal\n");
    		}
    		printf("Now we multiply both the above matrix \n");
    		printf("The result of the multiplication is as follows:\n");
    		/*a11xA11+a12xA21+a13xA31 a11xA12+a12xA22+a13xA32 a11xA13+a12xA23+a13xA33*/
    		for(i=0;i<r1;i++)
    		{
    			for(j=0;j<c2;j++)
    			{
    				mult[i][j]=0;
    				for(k=0;k<r1;k++)
    				{
    					mult[i][j]+=m1[i][k]*m2[k][j];
    					/*mult[0][0]=m1[0][0]*m2[0][0]+m1[0][1]*m2[1][0]+m1[0][2]*m2[2][0];*/
    				}
    				printf("%d\t",mult[i][j]);
    			}
    			printf("\n");
    		}
    		getch();
    	}
    	else
    	{
    		printf("Matrix multiplication cannot be done");
    	}
    }
     
    shabbir likes this.
  2. elena

    elena Banned

    Joined:
    May 20, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    I'm a student and I've got to realize matrix multiplication using threads. I know how to do it without threads, but with threads it seems to me a little confusing, cause I haven't used threads.
     
  3. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    If you are user of Visual Studio (VC8) then you can easily use this

    Code:
    
    #include<omp.h>
    
    omp_set_num_threads(2 or 4 or 6) // as depends on your loop
    #pragma omp parallel for shared(mult, r1,c2) private(j,k) 
     for(i=0;i<r1;i++)
            {
                for(j=0;j<c2;j++)
                {
                    mult[i][j]=0;
                    for(k=0;k<r1;k++)
                    {
                        mult[i][j]+=m1[i][k]*m2[k][j];
                        /*mult[0][0]=m1[0][0]*m2[0][0]+m1[0][1]*m2[1][0]+m1[0][2]*m2[2][0];*/
                    }
                   //printf("%d\t",mult[i][j]);
                }
                //printf("\n");
            }
    
    

    By the use of OpenMP, parallel computing will be done. Today Google is using OpenMP, MPI, Grid Computing.
     
    Dhananjay Sirsat and shabbir like this.
  4. wtfmate

    wtfmate New Member

    Joined:
    Jan 21, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    There is a bug in this code; the inner most for loop should be:

    for(k=0;k<r2;k++) { ... }
     
  5. mclark

    mclark New Member

    Joined:
    Mar 23, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I'm using xcode as my compiler, and it's stating that there are 2 bugs...
    the first bug is on the second line where the "{" is. Xcode is says, "Return type of 'main' is not 'int'"... what does this mean?

    and... the other bug is on the 72 line, where the getch() statement is.. xcode says, "Implicit declaration of function 'getch'"...

    any idea of how to correct these two bugs?
     
  6. tajender

    tajender New Member

    Joined:
    Mar 31, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    varanasi
    :nice::cuss:
     
  7. broncha

    broncha New Member

    Joined:
    Apr 10, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Web host administrator and developer
    Location:
    Kathmandu
    Home Page:
    http://www.rajesharma.com
    For the first bug change the data type of main from void to int . and at the end before closing main function "return 0"

    Code:
    int main()
    for the second bug :
    Are you including conio.h ?for getch() you should include conio.h
     
  8. 3eenjohn

    3eenjohn New Member

    Joined:
    Apr 24, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I have a very urgent Question ..... How to make a program which support brackets () and maths operations +-/* from user to input in a string ???
    eg : [ (2+4) , 5*7 , ...... ]
    Please it's urgent
    and if any one knew the answer please thankfully could he send the program to my e-mail ( dodo-makram@hotmail.com )
     
  9. user2390

    user2390 New Member

    Joined:
    Dec 22, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    the above codes has one logical error in multiplying 2 matrices......
    the correct portion of the codes:
    for(i=0;i<r1;i++)
    {
    for(j=0;j<c2;j++)
    {
    mult[j]=0;
    for(k=0;k<r1;k++)
    {
    ....
     
  10. user2390

    user2390 New Member

    Joined:
    Dec 22, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Excuse me ........
    the correct portion is:
    for(i=0;i<r1;i++)
    {
    for(j=0;j<c2;j++)
    {
    mult[j]=0;
    for(k=0;k<c1;k++)
    {
    ......
     
  11. gavinol

    gavinol New Member

    Joined:
    Jan 15, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Only matrices of the same order (same number of rows and same number of columns) may be added by adding corresponding elements.

    Example:

    1. [2 -3] [7 3] [2 + 7 -3 + 3] [9 0]


    [4 5] + [-2 7] = [4 + (-2) 5 + 7] = [2 12]


    [6 0] [1 -5] [6 + 1 0 + (-5)] [7 -5]


    2. [2 -3] [7 3 5]


    [4 5] + [-2 7 -9]


    [6 0] [1 -5 0]
    The addition above is undefined since the two matrices do not have the same number of columns.



     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice