0
4.0kviews
Write a program to perform matrix multiplication using user defined functions Assume first matrix is of size M x N, second matrix of size N x P and third matrix(Result matrix) is of M x P.

Mumbai University> FE > Sem 2> STRUCTURED PROGRAMMING APPROACH

Marks: 10 M

Year: May 2016

1 Answer
0
142views
Program:
#include<stdio.h>
#include<math.h>
#include<conio.h>
void read_matrix(int a[10][10],int m,int n);
void multiply_matrix(int a[10][10],int b[10][10],int c[10][10],int m,int n,int p);
void display_matrix(int a[10][10],int m,int n);

main()
{
int a[10][10],b[10][10],c[10][10],m,n,p;

printf("Enter number of rows of matrix 1: ");
scanf("%d",&m);
printf("Enter number of colomns of matrix 1: ");
scanf("%d",&n);
printf("Enter number of columns of matrix 2: ");
scanf("%d",&p);
read_matrix(a,m,n);
read_matrix(b,n,p);
multiply_matrix(a,b,c,m,n,p);
display_matrix(c,m,p);
getch();
}

void read_matrix(int a[10][10],int m,int n)
{
    int i,j;
    printf("Enter the elements of matrix:\n");
    for(i=0;i<m;i++)
    {
    for(j=0;j<n;j++)
    {
        printf("Enter Element: ");
        scanf("%d",&a[i][j]);
    }
    }
}

void multiply_matrix(int a[10][10],int b[10][10],int c[10][10],int m,int n,int p)
{
    int i,j,k,sum=0;
    for(i=0;i<m;i++)
    {
    for(j=0;j<n;j++)
    {
    for(k=0;k<p;k++)
    {
    sum=sum+((a[i][k])*(b[k][j]));
    c[i][j]=sum;
    }
    sum=0;
    }
    }
}

void display_matrix(int a[10][10],int m,int n)
{
    int i,j;
    for(i=0;i<m;i++)
    {
    for(j=0;j<n;j++)
    {
        printf("%d\t",a[i][j]);
    }
printf("\n");
    }
}

Output:
Enter number of rows of matrix1 : 2
Enter number of columns of matrix 1:2
Enter number of columns of matrix 2: 2
Enter elements of matrix:
Enter Element:1
Enter Element:2
Enter Element;3
Enter Element:2
Enter elements of matrix:
Enter Element:2
Enter Element:3
Enter Element;4
Enter Element:1
10  5
14  11
Please log in to add an answer.