Sunday, May 5, 2013

Write a C program that uses functions to perform the Matrix Multiplication to the 2 Given Matrixes


/*Write a C program that uses functions to perform the
Matrix Multiplication to the 2 Given Matrixes
*/
#include<stdio.h>
#include<conio.h>

void read_mat(int,int,int[10][10]);
void print_mat(int,int,int[10][10]);
void mul_mat(int,int,int,int[10][10],int[10][10]);

main()
{
    int a[10][10],b[10][10],i,j,m,n,p,q;
    clrscr();
    printf("Enter 1st Matrix Row size and Column size:");
    scanf("%d %d",&m,&n);
            read_mat(m,n,a);
    printf("Enter 2nd Matrix Row size and Column size:");
    scanf("%d %d",&p,&q);
        read_mat(p,q,b);

    if(n!=p)
    {
        printf("Mulatiplication Not Possible");
    }
    else
    {
        printf("After Maultiplication:\n");
        mul_mat(m,q,n,a,b);
    }
    getch();
}

void read_mat(int x,int y,int c[10][10])
{
    int i,j;
    printf("Enter Elements:");
    for(i=0;i<x;i++)
    for(j=0;j<y;j++)
    scanf("%d",&c[i][j]);
}

void print_mat(int x,int y,int c[10][10])
{
    int i,j;
    for(i=0;i<x;i++)
    {
        for(j=0;j<y;j++)
        printf("%5d",c[i][j]);
        printf("\n");
    }
}

void mul_mat(int x,int y,int z,int a[10][10],int b[10][10])
{
    int i,j,k,c[10][10];
    for(i=0;i<x;i++)
    {
        for(j=0;j<y;j++)
        {
             c[i][j]=0;
             for(k=0;k<z;k++)
             c[i][j]=c[i][j]+a[i][k]*b[k][j];
        }
    }
    print_mat(x,y,c);
}

output:
Enter 1st Matrix Row size and Column size:
2
2
Enter Elements:
1
2
3
4
Enter 2nd Matrix Row size and Column size:2
2
Enter Elements:
9
8
7
6
After Multiplication:
   23   20
   55   48

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More