This tutorial teaches about Matrix Multiplication. Matrix is an array of rows and columns. Matrix Multiplication is a binary operation and finally generates new matrix.
Matrix Multiplication ~ All Code Tips |
#include<stdio.h>
int main()
{
int m, n, p, q, i, j, k, sum = 0;
int matrix1[10][10], matrix2[10][10], matrix_mul[10][10];
printf("Enter No of rows and columns of matrix-1");
scanf("%d%d", &m, &n);
printf("Enter elements of matrix-1 : ");
for(i = 0; i < m; i++)
for(j = 0; j < n; j++)
scanf("%d", &matrix1[i][j]);
printf("Enter No of rows and columns of matrix-2");
scanf("%d%d", &p, &q);
if ( n != p )
printf("Matrix multiplication not possible");
else
{
printf("Enter elements of matrix-2 : ");
for(i = 0; i < p; i++)
for(j = 0; j < q; j++)
scanf("%d", &matrix2[i][j]);
for(i = 0; i < m; i++)
{
for(j = 0; j < q; j++)
{
for(k = 0; k < p; k++)
{
sum = sum + matrix1[i][k] * matrix2[k][j];
}
matrix_mul[i][j] = sum;
sum = 0;
}
}
printf("Product of Matrix-1 and Matrix-2 : \n");
for(i = 0; i < m; i++)
{
for(j = 0; j < q; j++)
printf("%d\t", matrix_mul[i][j]);
printf("\n");
}
}
return 0;
}
0 comments :
Post a Comment