Compute the Product of Two Matrices
#include
#include
#define maxrows 10
#define maxcols 10
void readmatrix(int arr[][maxcols], int m, int n);
void printmatrix(int arr[][maxcols], int m, int n);
void productmatrix(int array1[][maxcols], int array2[][maxcols],
int array3[][maxcols], int m, int n);
void main()
{
int array1[maxrows][maxcols], array2[maxrows][maxcols],
array3[maxrows][maxcols];
int m, n;
printf("Enter the value of m and n \n");
scanf("%d %d", &m, &n);
printf("Enter Matrix array1 \n");
readMatrix(array1, m, n);
printf("Matrix array1 \n");
printMatrix(array1, m, n);
printf("Enter Matrix array2 \n");
readMatrix(array2, m, n);
printf("Matrix B \n");
printMatrix(array2, m, n);
productMatrix(array1, array2, array3, m, n);
printf("The product matrix is \n");
printMatrix(array3, m, n);
}
void readMatrix(int arr[][maxcols], int m, int n)
{
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
scanf("%d", &arr[i][j]);
}
}
}
void printMatrix(int arr[][maxcols], int m, int n)
{
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%3d", arr[i][j]);
}
printf("\n");
}
}
void productMatrix(int array1[][maxcols], int array2[][maxcols],
int array3[][maxcols], int m, int n)
{
int i, j, k;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
array3[i][j] = 0;
for (k = 0; k < n; k++)
{
array3[i][j] = array3[i][j] + array1[i][k] *array2[k][j];
}
}
}
getch();
}
Output
Enter the value of m and n
3 3
Enter matrix array1
4 5 6
1 2 3
3 7 8
Matrix array1
4 5 6
2 3
7 8
Enter matrix array2
5 6 9
8 5 3
9 1
Matrix array2
6 9
8 5 3
2 9 1
The product matrix is
72103 57
27 43 18
87125 56
No comments:
Post a Comment