Program to Multiply Two Matrices Using Multi-dimensional Arrays - (C Programs)
In this tutorial, we'll discuss a C program to multiply two matrices using multi-dimensional arrays. Multiplying two matrices is a common mathematical operation that is used in a variety of applications.
Syntax
void multiplyMatrices(int firstMatrix[][MAX], int secondMatrix[][MAX], int result[][MAX], int rowsFirst, int colsFirst, int rowsSecond, int colsSecond);
Example
#include <stdio.h>
#define MAX 100
void multiplyMatrices(int firstMatrix[][MAX], int secondMatrix[][MAX], int result[][MAX], int rowsFirst, int colsFirst, int rowsSecond, int colsSecond);
int main()
{
int firstMatrix[MAX][MAX], secondMatrix[MAX][MAX], result[MAX][MAX], rowsFirst, colsFirst, rowsSecond, colsSecond, i, j, k;
printf("Enter rows and column for first matrix: ");
scanf("%d %d", &rowsFirst, &colsFirst);
printf("Enter rows and column for second matrix: ");
scanf("%d %d", &rowsSecond, &colsSecond);
while (colsFirst != rowsSecond)
{
printf("Error! cols of first matrix not equal to rows of second.\n");
printf("Enter rows and column for first matrix: ");
scanf("%d %d", &rowsFirst, &colsFirst);
printf("Enter rows and column for second matrix: ");
scanf("%d %d", &rowsSecond, &colsSecond);
}
printf("\nEnter elements of matrix 1:\n");
for (i = 0; i < rowsFirst; ++i)
{
for (j = 0; j < colsFirst; ++j)
{
printf("Enter elements a%d%d: ", i + 1, j + 1);
scanf("%d", &firstMatrix[i][j]);
}
}
printf("\nEnter elements of matrix 2:\n");
for (i = 0; i < rowsSecond; ++i)
{
for (j = 0; j < colsSecond; ++j)
{
printf("Enter elements b%d%d: ", i + 1, j + 1);
scanf("%d", &secondMatrix[i][j]);
}
}
multiplyMatrices(firstMatrix, secondMatrix, result, rowsFirst, colsFirst, rowsSecond, colsSecond);
printf("\nResultant matrix:\n");
for (i = 0; i < rowsFirst; ++i)
{
for (j = 0; j < colsSecond; ++j)
{
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
}
void multiplyMatrices(int firstMatrix[][MAX], int secondMatrix[][MAX], int result[][MAX], int rowsFirst, int colsFirst, int rowsSecond, int colsSecond)
{
int i, j, k;
for (i = 0; i < rowsFirst; ++i)
{
for (j = 0; j < colsSecond; ++j)
{
result[i][j] = 0;
}
}
for (i = 0; i < rowsFirst; ++i)
{
for (j = 0; j < colsSecond; ++j)
{
for (k = 0; k < colsFirst; ++k)
{
result[i][j] += firstMatrix[i][k] * secondMatrix[k][j];
}
}
}
}
Output
Enter rows and column for first matrix: 2 3
Enter rows and column for second matrix: 3 2
Enter elements of matrix 1:
Enter elements a1 1: 1
Enter elements a1 2: 2
Enter elements a1 3: 3
Enter elements a2 1: 4
Enter elements a2 2: 5
Enter elements a2 3: 6
Enter elements of matrix 2:
Enter elements b1 1: 1
Enter elements b1 2: 2
Enter elements b2 1: 3
Enter elements b2 2: 4
Enter elements b3 1: 5
Enter elements b3 2: 6
Resultant matrix:
22 28
49 64
Explanation
This program uses multi-dimensional arrays to multiply two matrices. The user is prompted to enter the dimensions of both matrices, and then enter the elements of both matrices. The multiplyMatrices function is called to calculate the product of the two matrices, and the result is displayed.
Use
Matrix multiplication is a common mathematical operation that is used in a variety of applications, such as data analysis, image processing, and machine learning. This program can be used as a building block for larger applications that involve matrix operations.
Summary
In this tutorial, we discussed a C program to multiply two matrices using multi-dimensional arrays. We covered the syntax, example, output, explanation, and use of the program. With this program, you can multiply two matrices using multi-dimensional arrays and perform advanced calculations in C language.