Monday, March 14, 2016

C Program to Multiply two Matrix Using Function

/*=================================================================================
 **
 **  File Name     : matMul.c
 **  Creation Date : Mon 14 Mar 2016 03:41:26 AM IST
 **  Last Modified : Mon 14 Mar 2016 05:46:23 AM IST
 **  Compoler      : gcc
 **  Author        : Manoj Kumar Patra, manojpatra.sit@gmail.com
 **  Organization  : SCIS, University of Hyderabad, India.
 **
 **===============================================================================*/

#include<stdio.h>
void insert_ele(int a[10][10],int row,int col)
{
    int i,j;
    for(i=1;i<=row;i++)
    {
        for(j=1;j<=col;j++)
        {
            printf("Enter Element of row=%d , column= %d : ",i,j);
            scanf("%d",&a[i][j]);
        }
    }
}

void mul_mat(int m1[10][10],int m2[10][10],int m3[10][10],int r1,int c1, int r2, int c2)
{
    int i,j,k,sum=0;
    for(i=0; i<r1; i++)
    {
        for(j=0; j<c2; j++)
        {
            m3[i][j]=0;
        }
    }
    for(i=1;i<=r1;i++)
    {
        for(j=1;j<=c2;j++)
        {
            for (k=1;k<=c1;k++)
            {
                sum = sum + (m1[i][k] * m2[k][j]);
            }
            m3[i][j]=sum;
            sum=0;
        }
    }
}

void print_mat(int m[10][10],int row,int col)
{
    int i,j;
    for(i=1;i<=row;i++)
    {
        for(j=1;j<=col;j++)
        {
            printf("%d ",m[i][j]);
        }
        printf("\n");
    }
}

main()
{
    int m1[10][10],m2[10][10],m3[10][10],row,col,r1,c1,r2,c2;
    printf("\n= = = = = = Matrix Multiplication = = = = = =\n\n");
    printf("Enter number of rows in first matrix: ");
    scanf("%d",&r1);
    printf("Enter number of colomns in second matrix: ");
    scanf("%d",&c1);
    insert_ele(m1,r1,c1);
    printf("\nThe elements of first matrix are:\n");
    print_mat(m1,r1,c1);
    printf("- - - - - - - - - - - - - - - - - - - - - - - - -\n");
    printf("\nEnter number of rows in second matrx: ");
    scanf("%d",&r2);
    while(r2 != c1)
    {
        printf("Error: Number of rows in 2nd matrix must be same as number of columns in 1st matrix\n");
        printf("\nEnter number of rows in second matrx: ");
        scanf("%d",&r2);
    }
    printf("Enter number of colomns in second matrix: ");
    scanf("%d",&c2);
    insert_ele(m2,r2,c2);
    printf("\nThe elements of the second matrix are:\n");
    print_mat(m2,r2,c2);
   
    /*----------------------------------------------------*/
    mul_mat(m1,m2,m3,r1,c1,r2,c2);
    printf("- - - - - - - - - - - - - - - - - - - - - - - - -\n");
    printf("\nThe resultant matrix is:\n");
    print_mat(m3,r1,c2);
    printf("- - - - - - - - - - - - - - - - - - - - - - - - -\n");
    printf("\n");
}

OutPut:

max@ubuntu:~$ gcc matMul.c -o matMul
max@ubuntu:~$ ./matMul

= = = = = = Matrix Multiplication = = = = = =

Enter number of rows in first matrix: 2
Enter number of colomns in second matrix: 4
Enter Element of row=1 , column= 1 : 1
Enter Element of row=1 , column= 2 : 2
Enter Element of row=1 , column= 3 : 3
Enter Element of row=1 , column= 4 : 4
Enter Element of row=2 , column= 1 : 4
Enter Element of row=2 , column= 2 : 3
Enter Element of row=2 , column= 3 : 2
Enter Element of row=2 , column= 4 : 1

The elements of first matrix are:
1 2 3 4
4 3 2 1
- - - - - - - - - - - - - - - - - - - - - - - - -

Enter number of rows in second matrx: 4
Enter number of colomns in second matrix: 3
Enter Element of row=1 , column= 1 : 1
Enter Element of row=1 , column= 2 : 2
Enter Element of row=1 , column= 3 : 3
Enter Element of row=2 , column= 1 : 4
Enter Element of row=2 , column= 2 : 5
Enter Element of row=2 , column= 3 : 6
Enter Element of row=3 , column= 1 : 3
Enter Element of row=3 , column= 2 : 2
Enter Element of row=3 , column= 3 : 1
Enter Element of row=4 , column= 1 : 4
Enter Element of row=4 , column= 2 : 5
Enter Element of row=4 , column= 3 : 6

The elements of the second matrix are:
1 2 3
4 5 6
3 2 1
4 5 6
- - - - - - - - - - - - - - - - - - - - - - - - -

The resultant matrix is:
34 38 42
26 32 38
- - - - - - - - - - - - - - - - - - - - - - - - -

No comments:

Post a Comment