C Program for Matrix Multiplication
Introduction:
A matrix is a set of numbers or a set of functions that are arranged into a rectangular array. The term "matrix of order m by n" refers to a rectangular array of m x n numbers that is organised into m rows and n columns.
Only when the first matrix's first column's number equals the second matrix's second row's number can two matrices be multiplied. The first row's number of rows and the second matrix's number of columns will appear in descending order in the product of the two matrices.
The sum of the products across some rows of A and the equivalent entries along some columns of B is the product of two matrices, A and B. Simply expressed, the result of the dot product of the first row of the first matrix and the first column of the second matrix will be the first element of the product matrix.
Algorithm for Multiplication of Matrix:
- Start.
- Enter m and n's values (or) the first matrix's order.
- Enter the order of the second matrix (or) the values of p and q.
- Make a matrix with the sizes a [m], n, and b [p], q.
- Loops are used to enter matrix elements row by row.
- Print matrix multiplication is not feasible and the programme will terminate if the first matrix's first column's number is not equal to the second matrix's second row's number. If not, move on to the next phase.
- Make a third matrix, c, with dimensions m x q to hold the finished good.
- From i = 0 to i = m, create a loop.
- For the loop above, add an inner loop that runs from j = 0 to j = q.
- Set the new matrix's element (i, j)'s value to 0 at the beginning.
- From k = 0 to k=p, create an inner loop inside the loop shown above.
- In the third matrix, c[i][j], enter the value of a[i][k]*b[k][j] using the add and assign operator (+=).
- To print, use the third matrix.
- Stop.
Multiplication of Square Matrices:
The term "square matrix" refers to a matrix where the number of rows and columns is equal. Only when both matrices have the same order may two square matrices of the same order be multiplied.
The matching elements are multiplied and the products are added to determine the product of the two matrices. With the aid of the example presented in the section titled "Introduction to Matrix Multiplication in C," it is considerably easier to comprehend.
Here is the code to calculate the product of two square matrices.
#include<stdio.h>
int main()
{
int p[10][10], q[10][10], r[10][10], n, i, j, k;
printf("Enter the value of N (N <= 10): ");
scanf("%d", & n);
printf("Enter the values of Matrix-A: \n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", & p[i][j]);
}
}
printf("Enter the values of Matrix-B: \n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", & q[i][j]);
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
r[i][j] = 0;
for (k = 0; k < n; k++) {
r[i][j] += p[i][k] * q[k][j];
}
}
}
printf("Product of Two Matrices: \n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%d\t", r[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT:
![C Program for Matrix Multiplication](https://static.tutorialandexample.com/c/c-program-for-matrix-multiplication1.png)
Multiplication of Rectangular Matrices:
The term "rectangular matrix" refers to a matrix where the number of rows differs from the number of columns.
Similar to the requirement for multiplying two square matrices, the first matrix's number of columns must match the second matrix's number of rows in order for the multiplication of two rectangular matrices to be compatible.
The code to determine the product of two rectangular matrices may be found below.
#include<stdio.h>
int main() {
int a,b, m, n, i, j, k;
int p[10][10], q[10][10], res[10][10];
printf("Enter the order of first matrix\n");
scanf("%d%d", & a, & b);
printf("Enter the order of second matrix\n");
scanf("%d%d", & m, & n);
if (b != m)
{
printf("Matrix is incompatible for multiplication\n");
} else
{
printf("Enter the elements of Matrix-A:\n");
for (i = 0; i < a; i++) {
for (j = 0; j < b; j++) {
scanf("%d", & p[i][j]);
}
}
printf("Enter the elements of Matrix-B:\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", & q[i][j]);
}
}
for (i = 0; i < a; i++) {
for (j = 0; j < n; j++) {
res[i][j] = 0;
for (k = 0; k < m; k++) {
res[i][j] += p[i][k] * q[k][j];
}
}
}
printf("Product of two matrices:-\n"); for (i = 0; i < a; i++) { for (j = 0; j < n; j++) { printf("%d\t", res[i][j]); } printf("\n"); } } return 0; }
OUTPUT:
![C Program for Matrix Multiplication](https://static.tutorialandexample.com/c/c-program-for-matrix-multiplication2.png)
Implementing Matrix Multiplication by using functions:
We will now discover how to multiply matrices by sending them to a function. Specifically, three tasks will be required. one that accepts the matrix as an input; let's call this function input().
Additionally, a function called display () is required in order to display the matrix's result. In order to calculate the product of the two matrices, we will need a function. Let's call this multiply ().
The matrix's elements will be passed as input to the input () function.
To print the matrix, we'll utilise the show () function.
Utilising the multiply () function, we can get the product of two matrices.
#include <stdio.h>
#include <stdlib.h>
#define r1 2
#define c1 3
#define r2 3
#define c2 2
void Matrix_multiplication (int p[][c1], int q[][c2])
{
int mul[r1][c2];
printf ("Product of matrices is:\n");
for (int i = 0; i < r1; i++)
{
for (int j = 0; j < c2; j++)
{
mul[i][j] = 0;
for (int k = 0; k < r2; k++)
{
mul[i][j] +=p[i][k] * q[k][j];
}
printf ("%d\t", mul[i][j]);
}
printf ("\n");
}
}
int main ()
{
int p[r1][c1] = { {1,2,1}, {1,2,1} };
int q[r2][c2] = { {1, 2}, {2, 1}, {1, 2} };
int mul[r1][c2], i, j, k;
printf ("Elements of matrix-A is :\n");
for (i = 0; i < r1; i++)
{
for (j = 0; j < c1; j++)
{
printf ("%d ", p[i][j]);
if (j == 3 - 1)
{
printf ("\n");
}
}
}
printf ("Elements of matrix-B is :\n");
for (i = 0; i < r2; i++)
{
for (j = 0; j < c2; j++)
{
printf ("%d ", q[i][j]);
if (j == 2 - 1)
{
printf ("\n");
}
}
}
Matrix_multiplication (p, q);
return 0;
}
OUTPUT:
![C Program for Matrix Multiplication](https://static.tutorialandexample.com/c/c-program-for-matrix-multiplication3.png)
Conclusion:
- Finding the product of two matrices is a laborious manual task that is made easier by C's matrix multiplication.
- We can practise applying mathematical logic to a coding algorithm by multiplying matrices in C.
- We can also quickly solve linear equations in C by multiplying matrices.