facebook like button

29 April, 2011

program 37: matrix multiplication

The need:
     This is a simple program to multiply 2 matrices.
The code: 
--------------------------------------------
#include<stdio.h>
main()
{
    int j,i,k,m1,n1,m2,n2;
    int A[20][25],B[20][25],mul[20][25];         //declaration of 2 dimensional array
    printf("This is a program to get multiplication of two matrices A nad B\n\n");
    printf("Enter the rows in mat A..\n");
    scanf("%d",&m1);
    printf("Enter the columns in mat A..\n");
    scanf("%d",&n1);
    printf("Now keep on entering matrix elements.\n\n");
    for (i=0;i<m1;i++)
    {
        for (j=0;j<n1;j++)
        {
            printf("A%d%d=",i+1,j+1);
            scanf("%d",&k);
            A[i][j]=k;
        }
    }
   
    printf("\nEnter the rows in mat B..\n");
    scanf("%d",&m2);
    if(n1!=m2)
    {
        printf("\n\nDimension error.\nCan not multiply\nexiting...\n");
        exit(0);
    }
    printf("Enter the columns in mat B..\n");
    scanf("%d",&n2);
    printf("Now keep on entering matrix elements.\n\n");
    for (i=0;i<m2;i++)
    {
        for (j=0;j<n2;j++)
        {
            printf("B%d%d=",i+1,j+1);
            scanf("%d",&k);
            B[i][j]=k;
        }
    }
   
    printf("\n\nMatrix A is\n\n");
    for (i=0;i<m1;i++)
    {
        for (j=0;j<n1;j++)
        {
            printf ("%d ",A[i][j]);
        }
    putchar('\n');
    }
   
    printf("\n\nMatrix B is\n\n");
    for (i=0;i<m2;i++)
    {
        for (j=0;j<n2;j++)
        {
            printf ("%d ",B[i][j]);
        }
    putchar('\n');
    }
   
   
   
    for (i=0;i<m1;i++)            //initialisation of mat mul
    {
        for (j=0;j<n2;j++)
        {
             mul[i][j]=0;
        }
    }
   
   
    for (i=0;i<m1;i++)            //multiplication part
    {
        for (j=0;j<n2;j++)
        {
            for (k=0;k<n1;k++)
            {
                mul[i][j]+=A[i][k]*B[k][j];
            }
        }
    }
   
   
    printf("\n\nThe multiplied matrix is\n\n");
    for (i=0;i<m1;i++)
    {
        for (j=0;j<n2;j++)
        {
            printf ("%d ",mul[i][j]);
        }
    putchar('\n');
    }
    getchar();
}
-------------------------------------------- 

The approach: 
This program shows how one can multiply 2 matrices. Run the program. This program asks you to enter the number of rows and columns of the matrix A, then matrix A and similarly matrix B. This also prints both the matrices for user. Then the program multiplies the 2 matrices and prints the output matrix. For multiplication of 2 matrices to be defined number of columns of first matrix should be equal to number of rows of second matrix. In the program does not find those to be equal, it terminates. The 'exit()' functions cause the program to terminate. Read remark for details. Multiplication procedure implemented here is the same as you people follow when you multiply 2 matrices. To know more about how a matrix is stored and printed .see program36.


Remarks:

1. You have noticed that I have written a 0 in parentheses of 'exit()' function. Actually any integer could be written there or even it could be left blank.
2. The number helps in determining the site of termination of the program at run time when there are multiple 'exit()' statements in it.

No comments:

Post a Comment

feel free to ask your doubts... if any
you can post your doubts on
www.facebook.com/programsimply