C Program for Matrix Multiplication

The ordinary matrix multiplication is the most often used and the most important way to multiply matrices. It is defined between two matrices only if the width of the first matrix equals the height of the second matrix. Multiplying an m×n matrix with an n×p matrix results in an m×p matrix.

The C program below show the matrix multiplication. The exe file can be downloaded from here : Matrix Multiplication

Programming Geeks : C Program for Matrix Multiplication

# include "stdio.h"
main()
{
    int m1[10][10],i,j,k,m2[10][10],mult[10][10],r1,c1,r2,c2;
    printf("Enter number of rows and columns of first matrix (less than 10)\n");
    scanf("%d%d",&r1,&c1);
    printf("Enter number of rows and columns of second matrix (less than 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("First Matrix is :\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("Row wise\n");
        for(i=0;i<r2;i++)
            for(j=0;j<c2;j++)
                scanf("%d",&m2[i][j]);
        printf("Second Matrix is:\n");
        for(i=0;i<r2;i++)
        {
            for(j=0;j<c2;j++)
                printf("%d\t",m2[i][j]);
            printf("\n");
        }
        printf("Multiplication of the Matrices:\n");
        for(i=0;i<r1;i++)
        {
            for(j=0;j<c2;j++)
            {
                mult[i][j]=0;
                for(k=0;k<c1;k++)
                    mult[i][j]+=m1[i][k]*m2[k][j];
                printf("%d\t",mult[i][j]);
            }
            printf("\n");
        }
    }
    else
    {
        printf("Matrix multiplication cannot be done");
    }
    return 0;
}


Related Posts

Share

Filed Under: CCodes

About the Author: Software Engineer - Advanced Search & Recommendation at Rovi

RSSComments (20)

Leave a Reply | Trackback URL

  1. Do you mind if I use the information I got reading from this website on the project that I’m working? I will properly cite you.

  2. Prabhat says:

    Thanks to provide the solution……of multiplication of array…

  3. Bruce Warrior says:

    Thanks you! This was useful for me! It’s clear and easy to understand

  4. online games says:

    your code for mat multi was useful…thanks’

  5. Bibhuti says:

    Really very helpful………………

  6. nikhil gupta says:

    thanx…..really useful for quick revision.

  7. shwetha says:

    Thanks for the code……

  8. Sandy says:

    Thanksssss da its very easy to understand

  9. Miki says:


    for(k=0;k<r1;k++)
    mult[i][j]+=m1[i][k]*m2[k][j];

    Not good.

    It should be:

    for(k=0;k<c1;k++)
    mult[i][j]+=m1[i][k]*m2[k][j];

  10. Miki is right. The counter of third loop in multiplication must be less then column1 not row1.

    for(k=0;k<c1;k++) /* corrected code */

  11. ramjanam prasad says:

    thanks

  12. thomess says:

    Miki is corcrect . k should have the counter of c1=r2, not R1.

  13. Nidhin says:

    Thanks for providing this code.It works really well.

  14. peter says:

    nice code dude
    will anybody suggest me good books for matlab in image processing.

  15. firoz says:

    correction:
    in the for command line
    for(k=0;k<r1;k++);
    the condition given is wrong…
    it should be either k<r2 or k<c1
    plzz correct it..

  16. Saurabh says:

    Finally I’m out of hibernation ;)
    Thanks Firoz, Miki and others for pointing out the mistake.
    I’ve corrected it now.

  17. sam says:

    i still think this code can be further improved since u see with this code a matrix of (r1Xc1)=(2X3)&(r2Xc2)=(2X2) can not be multiplied as the code processes only when (c1==r2). while we all know that the 2 matrices i mentioned above can be multiplied…..

Leave a Reply

*

AWSOM Powered