Matix multiplication program is one of the common program, we should use to master the C concepts, usually calculating matix multiplications requires large number of calculations but in C , we can do it easily and efficiently.
To do matrix multiplication in C, we have two possible ways, using pointer and without pointers, it can sub-divided into using functions and without using functions.
We will be creating two programs here, one will be without using functions/pointers and the other one passes matrices to functions and uses pointers.
Understanding Matrix multiplication
For matrix multiplication C program, it can only and only possible if the column of the first matrix should be equal to the row of the second, you can understand the matrix multiplication concept using the image below.
Let's take a complete example to understand it, suppose we have (3×3) × (3×3) and since the number of columns in A is the same as the number of rows in B (the middle two numbers are both 3 in this case), we can go ahead and multiply these matrices. Our result will be a (3×3) matrix.
We multiply the individual elements along the first row of matrix A with the corresponding elements down the first column of matrix B, and add the results. This gives us the number we need to put in the first row, first column position in the answer matrix.
we will repeat the above procedure for all the row of A to multiple with all of the columns of B, thus we will get the result as below
Matrix multiplication program in C (Without using function/pointers)
Now, as we have understood the concepts of matrix multiplication, we can create the Code in C for this
#include<stdio.h>
int main()
{
int a[10][10],b[10][10],result[10][10],m,n,p,q,i,j,k;
//get the nuumber of rows/columns of first matrix
printf("Enter rows and columns of first matrix:");
scanf("%d%d",&m,&n);
//get the nuumber of rows/columns of second matrix
printf("Enter rows and columns of second matrix:");
scanf("%d%d",&p,&q);
//check if rows of A qual to columns of B
if(n==p)
{
printf("\nEnter first matrix:\n");
//fetch the first matrix data from user and save in array a[10][10] declared above
for(i=0;i<m;++i)
{
for(j=0;j<n;++j)
{
scanf("%d",&a[i][j]);
}
}
printf("\nEnter second matrix:\n");
//fetch the second matrix data from user and save in array b[10][10]
for(i=0;i<p;++i)
{
for(j=0;j<q;++j)
{
scanf("%d",&b[i][j]);
}
}
printf("\nThe multiplied matrix result is:\n");
//multiply the matrix and save it in result[][] array & print it
for(i=0;i<m;++i)
{
for(j=0;j<q;++j)
{
result[i][j]=0;
for(k=0;k<n;++k)
result[i][j]=result[i][j]+(a[i][k]*b[k][j]);
printf("%d ",result[i][j]);
}
printf("\n");
}
}
else
{
printf("\n Matrix multiplication can't be done for these rows/columns");
}
return 0;
}
I have explained most of the coding lines by using comments, executing the above code on an online compiler provides me output as below
Enter rows and columns of first matrix:3
3
Enter rows and columns of second matrix:3
3
Enter first matrix:
11
22
33
44
55
66
77
88
99
Enter second matrix:
11
22
33
44
55
66
77
88
99
The multiplied matrix result is:
3630 4356 5082
7986 9801 11616
12342 15246 18150
For understanding code, I have commented out a few important lines of code to explain it.
Matrix multiplication in C using pointers and functions
Now in this program, we will be doing matrix multiplication using Pointers and functions, concept and logic are the same, we have just divided the code's into functions and used pointers, I have explained the important part of the code using comments.
#include<stdio.h>
#define ROW 3
#define COL 3
int main()
{
int First[3][3],Second[3][3],Result[3][3];
int m1,n1,m2,n2,i,j;
printf("Enter no of row and column for 1st matrix\n");
scanf("%d%d",&m1,&n1);
printf("Enter no of row and column for 2nd matrix\n");
scanf("%d%d",&m2,&n2);
//check if multiplication is possible, 1st matrix rows must be equal to
//Second matrix columns
if(n1==m2)
{
printf("Enter First matrix\n");
matrixInput(First);
printf("\nEnter Second matrix\n");
matrixInput(Second);
matrixMultiply(First,Second,Result);
printf("Product of entered matrices :-\n");
//print the resultant matrices
for(i=0;i<m1;i++)
{
for(j=0;j<n2;j++)
printf("%d\t",*(*(Result + i) + j));
printf("\n");
}
}
else
{
printf("Sorry ! Multiplication is not possible");
}
return 0;
}
//Get matrix data
void matrixInput(int mat[][COL])
{
int row, col;
for (row = 0; row < ROW; row++)
{
for (col = 0; col < COL; col++)
{
scanf("%d", (*(mat + row) + col));
}
}
}
//Function to multiply Matrices mat1 * mat2 = res
void matrixMultiply(int mat1[][COL], int mat2[][COL], int res[][COL])
{
int row, col, i;
int sum;
for (row = 0; row < ROW; row++)
{
for (col = 0; col < COL; col++)
{
sum = 0;
// find the sum of product of each elements of row for first matirx
// and columsn of second
for (i = 0; i < COL; i++)
{
sum += (*(*(mat1 + row) + i)) * (*(*(mat2 + i) + col));
}
//save sum in resultant matrix
*(*(res + row) + col) = sum;
}
}
}
Output of the above code after compiling here https://onlinegdb.com/BywHbCJlm
Enter no of row and column for 1st matrix
3 3
Enter no of row and column for 2nd matrix
3 3
Enter First matrix
10 20 30
40 50 60
70 80 90
Enter Second matrix
10 20 30
40 50 60
70 80 90
Product of entered matrices :
3000 3600 4200
6600 8100 9600
10200 12600 15000
You may also like:
I hope the above program helped you today, if you have any questions feel free to ask them in the comment section.