Let us see how to multiply two matrices. Here is the rule applicable for the multiplication of two matrices. Multiplication of two matrices is possible only if the number of columns of the first matrix is equal to the number of rows of the second matrix.
For example it is possible to multiply two matrices with size 2 x 2 and 2 x 3. Here number of columns of first matrix ie. 2 is equal to the number of rows of the second matrix that is again 2.
Example:
C program for matrix multiplication
matmult.c
#include <stdio.h>
int main() {
int m, n, p, q, i, j, k, sum = 0;
int a[10][10], b[10][10], c[10][10];
clrscr();
// Input order or size of first matrix
printf("Enter the number of rows and columns of first matrix:");
scanf("%d%d", &m, &n);
// Input first matrix
for (i=0;i<m;i++)
for (j=0;j<n;j++) {
printf("Enter the element %d, %d of matrix a:", i,j);
scanf("%d", &a[i][j]);
}
// Input order or size of second matrix
printf("\nEnter the number of rows and columns of second matrix:");
scanf("%d%d", &p, &q);
// Check whether the matrices are compatible for multiplication
if (n!=p)
printf("\nMatrices with entered orders can't be multiplied with each other.");
else {
// Input second matrix
for (i=0;i<p;i++)
for (j=0;j<q;j++) {
printf("Enter the %d, %d element of matrix b:", i, j);
scanf("%d", &b[i][j]);
}
// Logic for multiplication
for (i=0;i<m;i++) { // Loops row times of first matrix
for (j=0;j<q;j++) { // Loops column times of second matrix
for (k=0;k<p;k++) { // Loops row times of second matrix
sum=sum+a[i][k]*b[k][j];
}
c[i][j] = sum;
sum = 0;
}
}
// Displays first matrix
printf("Matrix A:\n");
for (i=0;i<m;i++) {
for (j=0;j<n;j++)
printf("%d\t", a[i][j]);
printf("\n");
}
// Displays second matrix
printf("Matrix B:\n");
for (i=0;i<p;i++) {
for (j=0;j<q;j++)
printf("%d\t", b[i][j]);
printf("\n");
}
// Displays third matrix
printf("Product of entered matrices:\n");
for (i=0;i<m;i++) {
for (j=0;j<q;j++)
printf("%d\t", c[i][j]);
printf("\n");
}
}
return 0;
}
For example it is possible to multiply two matrices with size 2 x 2 and 2 x 3. Here number of columns of first matrix ie. 2 is equal to the number of rows of the second matrix that is again 2.
Example:
C program for matrix multiplication
matmult.c
#include <stdio.h>
int main() {
int m, n, p, q, i, j, k, sum = 0;
int a[10][10], b[10][10], c[10][10];
clrscr();
// Input order or size of first matrix
printf("Enter the number of rows and columns of first matrix:");
scanf("%d%d", &m, &n);
// Input first matrix
for (i=0;i<m;i++)
for (j=0;j<n;j++) {
printf("Enter the element %d, %d of matrix a:", i,j);
scanf("%d", &a[i][j]);
}
// Input order or size of second matrix
printf("\nEnter the number of rows and columns of second matrix:");
scanf("%d%d", &p, &q);
// Check whether the matrices are compatible for multiplication
if (n!=p)
printf("\nMatrices with entered orders can't be multiplied with each other.");
else {
// Input second matrix
for (i=0;i<p;i++)
for (j=0;j<q;j++) {
printf("Enter the %d, %d element of matrix b:", i, j);
scanf("%d", &b[i][j]);
}
// Logic for multiplication
for (i=0;i<m;i++) { // Loops row times of first matrix
for (j=0;j<q;j++) { // Loops column times of second matrix
for (k=0;k<p;k++) { // Loops row times of second matrix
sum=sum+a[i][k]*b[k][j];
}
c[i][j] = sum;
sum = 0;
}
}
// Displays first matrix
printf("Matrix A:\n");
for (i=0;i<m;i++) {
for (j=0;j<n;j++)
printf("%d\t", a[i][j]);
printf("\n");
}
// Displays second matrix
printf("Matrix B:\n");
for (i=0;i<p;i++) {
for (j=0;j<q;j++)
printf("%d\t", b[i][j]);
printf("\n");
}
// Displays third matrix
printf("Product of entered matrices:\n");
for (i=0;i<m;i++) {
for (j=0;j<q;j++)
printf("%d\t", c[i][j]);
printf("\n");
}
}
return 0;
}
No comments:
Post a Comment