## 1.10.2012

### Product of matrix

Q. Write a C program to accept value of matrix and find out the product of matrix.
OR
Q. Give a example of 2d array with suitable arithmetic operation.

Ans.

/*source code of product of two matrix c program*/
#include<stdio.h>
#include<conio.h>
#define MAX 3
void input_mat(int [MAX][MAX], int, int);
void show_mat(int [MAX][MAX], int, int);
void prod_mat(int [MAX][MAX],int [MAX][MAX],int [MAX][MAX],int,int);
int main()
{
int x[MAX][MAX],y[MAX][MAX],z[MAX][MAX];
int row,col;
printf("Enter no. of rows and columns : ");
scanf("%d%d",&row, &col);
printf("Enter values in first matrix :\n");
input_mat(x,row,col);
printf("Enter values in second matrix :\n");
input_mat(y,row,col);
prod_mat(x,y,z,row,col);
printf("\nDisplay product of two matrices :\n");
show_mat(z,row,col);
getch();
return 0;
}
void input_mat(int matA[MAX][MAX], int r, int c)
{
int i,j;
for(i=0; i<r; i++)
{
for(j=0;j<c; j++)
scanf("%d",&matA[i][j]);
}
}
void prod_mat(int matA[MAX][MAX],int matB[MAX][MAX],int matC[MAX][MAX],int r,int c)
{
int i,j,k;
for(i=0; i<r; i++)
{
for(j=0;j<c; j++)
{
matC[i][j]=0;
for(k=0;k<c; k++)
{
matC[i][j]=matC[i][j]+matA[i][j]*matB[i][j];
}
}
}
}
void show_mat(int mat[MAX][MAX], int r, int c)
{
int i,j;
for(i=0; i<r; i++)
{
for(j=0;j<c; j++)
printf(" %d",mat[i][j]);
printf("\n");
}
}
/************************************
The output of above program would be:
*************************************/
 Figure: Screen shot of product of two matrix C program

Output in simple and easy way :-
Enter no. of rows and columns : 3 3
Enter values in First matrix :
4 5 3
2 1 9
6 7 5
Enter values in second matrix :
1 3 2
7 6 4
2 1 0

Display product of two matrices :
12 45 18
42 18 108
36 21 0

You might also like to read: