Matrix multiplication program in C
In this post, you will learn different ways to perform matrix multiplication using the C programming language.
Matrix, a set of numbers arranged in rows and columns so as to form a rectangular array. Matrices are utilised substantially more in everyday life than most would have suspected. A square matrix can represent a linear transformation of a geometric object. A real-life example is Adobe Photoshop. It uses a matrix to process linear transformations to render images. In robotics and automation, matrices are the fundamental building blocks for robot development. The contributions for controlling robots are acquired based on the calculations from matrices.
The different operations on the matrix are also very important for us. Let us discuss different ways to perform matrix multiplication-
C matrix multiplication using user inputs
In the given C program, we ask the user to provide the number of rows and columns and their elements. Then perform matrix multiplication.
#include<stdio.h>
#include<stdlib.h>
int main(){
int a[10][10],b[10][10],mul[10][10],row,col,i,j,k;
system("cls");
printf("Enter the number of row: ");
scanf("%d",&row);
printf("Enter the number of column: ");
scanf("%d",&col);
printf("Enter the first matrix element: \n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the second matrix element: \n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("Multiplication of the matrix: \n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
mul[i][j]=0;
for(k=0;k<col;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
// displaying result
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
}
Output of the above code:
Enter the number of row: 3
Enter the number of column: 3
Enter the first matrix element:
2 4 6
8 9 3
7 8 2
Enter the second matrix element:
9 3 2
7 3 1
2 5 4
Multiplication of the matrix:
58 48 32
141 66 37
123 55 30
C matrix multiplication using for loop
A nested loop is a loop inside a loop. In the given C program, we have used the nested for loops to iterate through each row and each column and multiply two matrices of 3 rows and 3 columns.
#include<stdio.h>
#include<stdlib.h>
int main(){
int i,j,k;
system("cls");
//creating two matrices
int a[3][3]={{3,2,7},{1,6,2},{7,2,3}};
int b[3][3]={{3,2,8},{2,4,6},{2,3,6}};
//creating a new matrix to store the product of two matrices
int mul[3][3];
printf("Multiplication of the matrix: \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
mul[i][j]=0;
for(k=0;k<3;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
//for printing result
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
}
Output of the above code:
Multiplication of the matrix:
27 35 78
19 32 56
31 31 86
C matrix multiplication by Passing it to a Function
In the given C program, using functions, performs matrix multiplication of two matrices and displays the result.
#include<stdio.h>
// function to get matrix elements entered by the user
void matrixElements(int matrix[][10], int row, int column) {
printf("\nEnter elements: \n");
for (int i = 0; i < row; ++i) {
for (int j = 0; j < column; ++j) {
printf("Enter a%d%d: ", i + 1, j + 1);
scanf("%d", &matrix[i][j]);
}
}
}
// function to multiply two matrices
void multiplyMatrices(int matrix1[][10],
int matrix2[][10],
int result[][10],
int row1, int col1, int row2, int col2) {
// Initializing elements of matrix mult to 0.
for (int i = 0; i < row1; ++i) {
for (int j = 0; j < col2; ++j) {
result[i][j] = 0;
}
}
// Multiplying first and second matrices and storing it in result
for (int i = 0; i < row1; ++i) {
for (int j = 0; j < col2; ++j) {
for (int k = 0; k < col1; ++k) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
}
// function to display the matrix
void display(int result[][10], int row, int column) {
printf("\nOutput Matrix:\n");
for (int i = 0; i < row; ++i) {
for (int j = 0; j < column; ++j) {
printf("%d ", result[i][j]);
if (j == column - 1)
printf("\n");
}
}
}
int main() {
int matrix1[10][10], matrix2[10][10], result[10][10], row1, col1, row2, col2;
printf("Enter rows and column for the first matrix: ");
scanf("%d %d", &row1, &col1);
printf("Enter rows and column for the second matrix: ");
scanf("%d %d", &row2, &col2);
// Taking input until
// 1st matrix columns is not equal to 2nd matrix row
while (col1 != row2) {
printf("Error! Enter rows and columns again.\n");
printf("Enter rows and columns for the first matrix: ");
scanf("%d%d", &row1, &col1);
printf("Enter rows and columns for the second matrix: ");
scanf("%d%d", &row2, &col2);
}
// get elements of the first matrix
matrixElements(matrix1, row1, col1);
// get elements of the second matrix
matrixElements(matrix2, row2, col2);
// multiply two matrices.
multiplyMatrices(matrix1, matrix2, result, row1, col1, row2, col2);
// display the result
display(result, row1, col2);
return 0;
}
Output of the above code:
Enter rows and column for matrix1: 2 2
Enter rows and column for matrix2: 2 2
Enter elements:
Enter a11: 3
Enter a12: 7
Enter a21: 5
Enter a22: 7
Enter elements:
Enter a11: 4
Enter a12: 2
Enter a21: 6
Enter a22: 7
Output Matrix:
54 55
62 59
Related Articles
Prime factors of a number in cArmstrong number program in c
Write a program to check leap year in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program to find greatest of three numbers
C program for addition of two numbers
C program to calculate compound interest
C program to find the ASCII value of a character
C program to convert Decimal to Octal
C program to convert decimal to binary
Write a C program to calculate Simple Interest
C program to check whether a number is even or odd
C program to reverse a number
C program to check palindrome number
C program to check whether an alphabet is a vowel or consonant
Program to find square root of a number in C
C program to check whether a number is positive or negative