A program for matrix multiplication using separate function to read, add and display.
A program for matrix multiplication using separate function to read, add and display.
Codes
/*A program for matrix multiplication using separate function to read, add and display*/
/*Programed by Jitendra Kr Yadav
10116
22 Mar,2011*/
#include<stdio.h>
#include<conio.h>
void read(int matrix[][20],int,int);
void mul(int matrix1[][20],int matrix2[][20],int,int);
void display(int matrix[][20],int,int);
main()
{
int matrix1[20][20];
int matrix2[20][20];
int i,j,row1,row2,col1,col2;
printf(“Enter the dimension of matrix1:\n”);
scanf(“%d%d”,&row1,&col1);
printf(“Enter the dimension of matrix2:\n”);
scanf(“%d%d”,&row2,&col2);
if((row1==row2)&&(col1==col2))
{
read(matrix1,row1,col1);
read(matrix2,row2,col2);
mul(matrix1,matrix2,row1,col1);
}
else
printf(“Matrix multiplication is not possible”);
getch();
}
void read(int matrix[][20],int row,int col)
{
int i,j;
printf(“Enter the elements of matrix:\n”);
for(i=0;i<row;i++)
for(j=0;j<col;j++)
scanf(“%d”,&matrix[i][j]);
}
void mul(int matrix1[][20],int matrix2[][20],int row,int col)
{
int matrix[20][20];
int i,j,k;
for(i=0;i<row;i++)
for(j=0;j<col;j++)
{
matrix[i][j]=0;
for(k=0;k<3;k++)
matrix[i][j]+=matrix1[i][j]*matrix2[i][j];
}
display(matrix,row,col);
}
void display(int matrix[][20],int row,int col)
{
int i,j;
printf(“The product of two matrix is:”);
for(i=0;i<row;i++)
{
printf(“\n”);
for(j=0;j<col;j++)
printf(“%d\t”,matrix[i][j]);
}
printf(“\n\n”);
printf(“Press any key to continue…”);
}
Output
Enter the dimension of matrix1:
3
3
Enter the dimension of matrix2:
3
3
Enter the elements of matrix:
1
2
3
4
5
6
7
8
9
Enter the elements of matrix:
9
8
7
6
5
4
3
2
1
The product of two matrix is:
27 48 63
72 75 72
63 48 27
Press any key to continue…