One of the very popular programs in C programming is Matrix Multiplication in c. The manual method of multiplication procedure involves a large number of calculations especially when it comes to a higher order of matrices, whereas a program in C can carry out the operations with short, simple, and understandable codes.
Matrix Multiplication in C can be done in two ways: without using functions and bypassing matrices into functions. In this post, we’ll discuss the source code for both these methods with sample outputs for each.
The source codes of these two programs for Matrix Multiplication in C programming are to be compiled in Code::Blocks. Running them on Turbo C and other platforms might require a few modifications to the code.
Introduction to Matrix Multiplication in C:
A matrix is a rectangular array of numbers, each of whose rows and columns corresponds to the same number of elements.
Multiplication of two matrices – When we multiply two matrices, it means to combine the corresponding rows and columns of the first matrix with the corresponding rows and columns of the second matrix.
You probably know how to multiply two matrices. Anyway, I’ve presented these three pictures below which clearly show how matrix multiplication takes place. The same idea as shown in these pictures has been followed in the same order in the program source codes for Matrix Multiplication in C. (For matrix multiplication, the column of the first matrix should be equal to the row of the second.)
Consider two matrices A and B of order 3×3 as shown below. Let’s denote the elements of matrix A by aij and those of matrix B by bij as shown below. These aij and bij are asked as inputs in the form of arrays in C program for Matrix Multiplication.
Let the resultant matrix upon multiplication of A and B be X with elements denoted by xij as shown.
The matrix multiplication takes place as shown below, and this same procedure is is used for multiplication of matrices using C.
Solving the procedure manually would require nine separate calculations to obtain each element of the final matrix X. These nine separate calculations have been done using very few lines of code involving loops and function in this C program for Matrix Multiplication.
Matrix Multiplication in C without using function:
//Source Code for Matrix Multiplication in C without using function
#include <stdio.h>
int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
printf("\nEnter the number of rows and columns of first matrix:\n");
scanf("%d%d", &m, &n);
/*//Entering elements of first matrix
printf("\nEnter the elements of first matrix\n");
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
scanf("%d", &first[c][d]);*/
printf("\nEnter the number of rows and columns of second matrix:\n");
scanf("%d%d", &p, &q);
//Checking if Matrix Multiplication is possible
if ( n != p )
{
printf("\nMatrices with entered orders can't be multiplied with each other.\n");
printf("\nThe column of first matrix should be equal to row of second.\n");
}
else
{
//Entering elements of first matrix
printf("\nEnter the elements of first matrix:\n");
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
scanf("%d", &first[c][d]);
//Entering elements of second matrix
printf("\nEnter the elements of second matrix:\n");
for ( c = 0 ; c < p ; c++ )
for ( d = 0 ; d < q ; d++ )
scanf("%d", &second[c][d]);
//Carrying out matrix multiplication operation
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
{
for ( k = 0 ; k < p ; k++ )
{
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}
//Printing the final product matrix
printf("\nThe product of entered matrices is:\n");
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
printf("%d\t", multiply[c][d]);
printf("\n");
}
}
return 0;
}
The above Matrix Multiplication in C program first asks for the order of the two matrices. If in the entered orders, the column of first matrix is equal to the row of second matrix, the multiplication is possible; otherwise, new values should be entered in the program.
The program then asks for the respective elements of the two matrices and multiplies them using loops as shown in the program. Finally, the resultant matrix obtained upon multiplication is printed. The final output screen is:
Matrix Multiplication in C by passing arrays to functions:
//Source Code for Matrix Multiplication in C by passing arrays to functions
#include <stdio.h>
void take_data(int a[][10], int b[][10], int r1,int c1, int r2, int c2);
void multiplication(int a[][10],int b[][10],int mult[][10],int r1,int c1,int r2,int c2);
void display(int mult[][10], int r1, int c2);
int main()
{
int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k;
printf("Enter rows and column for first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for second matrix: ");
scanf("%d %d",&r2, &c2);
//Checking if matrix multiplication is possible
while (c1!=r2)
{
printf("\nMatrices with entered orders can't be multiplied with each other.");
printf("\nMake the column of the first matrix equal to the row of the second.\n");
printf("\nEnter rows and column for first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for second matrix: ");
scanf("%d %d",&r2, &c2);
}
take_data(a,b,r1,c1,r2,c2);
multiplication(a,b,mult,r1,c1,r2,c2);
display(mult,r1,c2);
return 0;
}
//This matrix takes the data of matrices.
void take_data(int a[][10], int b[][10], int r1,int c1, int r2, int c2)
{
int i,j;
printf("\nEnter elements of matrix 1:\n");
for(i=0; i<r1; ++i)
for(j=0; j<c1; ++j)
{
printf("Enter elements a%d%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
printf("\nEnter elements of matrix 2:\n");
for(i=0; i<r2; ++i)
for(j=0; j<c2; ++j)
{
printf("Enter elements b%d%d: ",i+1,j+1);
scanf("%d",&b[i][j]);
}
}
//This function multiplies the entered matrices.
void multiplication(int a[][10],int b[][10],int mult[][10],int r1,int c1,int r2,int c2)
{
int i,j,k;
/* Initializing elements of matrix mult to 0.*/
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
{
mult[i][j]=0;
}
/* Multiplying matrix a and b and storing in array mult. */
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
for(k=0; k<c1; ++k)
{
mult[i][j]+=a[i][k]*b[k][j];
}
}
//This function displays the final matrix after multiplication.
void display(int mult[][10], int r1, int c2)
{
int i, j;
printf("\nThe product of the entered matrices is:\n");
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
{
printf("%d ",mult[i][j]);
if(j==c2-1)
printf("\n\n");
}
}
The operations involving loops and calculations in this program are similar to those in the previous one. The only difference is that this Matrix Multiplication program in C uses functions to pass arrays of matrices. There are three separate user-defined functions in this program to read data, preform matrix multiplication operation and display the resultant matrix.
Both these source codes are bug-free and have been tested on Code::Blocks with the inputs as shown in the output screens. I personally prefer to use functions to perform matrix multiplication. If you have any queries and feedbacks regarding these program source codes for Matrix Multiplication in C, you can mention and discuss them in the comments box below.
You can find more C Tutorials here.