Write a Program to Multiply Two Matrices in C
Matrix multiplication is a fundamental operation in linear algebra and finds applications in numerous fields like computer graphics, engineering, and scientific computing. In this blog post, we’ll write a C program to multiply two matrices and discuss the logic behind it.
1. Introduction to Matrix Multiplication
Multiplying two matrices involves taking the rows of the first matrix and performing dot products with the columns of the second matrix. The result is a new matrix with the number of rows equal to the first matrix and the number of columns equal to the second matrix. However, the number of columns in the first matrix must match the number of rows in the second matrix for matrix multiplication to be possible.
If A
is a matrix of size m x n
and B
is a matrix of size n x p
, then their product C
will be a matrix of size m x p
.
2. Writing the Program
Let’s write the C program to multiply two matrices. We will ask the user to input the elements of both matrices, and then we will compute and display the product.
Program to Multiply Two Matrices
#include <stdio.h>
int main() {
int firstMatrix[10][10], secondMatrix[10][10], resultMatrix[10][10];
int row1, col1, row2, col2;
// Input dimensions of first matrix
printf("Enter rows and columns of first matrix: ");
scanf("%d %d", &row1, &col1);
// Input dimensions of second matrix
printf("Enter rows and columns of second matrix: ");
scanf("%d %d", &row2, &col2);
// Check if matrix multiplication is possible
if (col1 != row2) {
printf("Error: Matrix multiplication not possible. The number of columns in the first matrix must equal the number of rows in the second matrix.
");
return 1;
}
// Input elements of first matrix
printf("Enter elements of the first matrix:
");
for (int i = 0; i < row1; i++) {
for (int j = 0; j < col1; j++) {
printf("Element at [%d][%d]: ", i, j);
scanf("%d", &firstMatrix[i][j]);
}
}
// Input elements of second matrix
printf("Enter elements of the second matrix:
");
for (int i = 0; i < row2; i++) {
for (int j = 0; j < col2; j++) {
printf("Element at [%d][%d]: ", i, j);
scanf("%d", &secondMatrix[i][j]);
}
}
// Initialize result matrix to zero
for (int i = 0; i < row1; i++) {
for (int j = 0; j < col2; j++) {
resultMatrix[i][j] = 0;
}
}
// Multiply the matrices
for (int i = 0; i < row1; i++) {
for (int j = 0; j < col2; j++) {
for (int k = 0; k < col1; k++) {
resultMatrix[i][j] += firstMatrix[i][k] * secondMatrix[k][j];
}
}
}
// Display the result
printf("
Resultant Matrix after multiplication:
");
for (int i = 0; i < row1; i++) {
for (int j = 0; j < col2; j++) {
printf("%d ", resultMatrix[i][j]);
}
printf("
");
}
return 0;
}
Explanation of the Code
Here’s a breakdown of how the program works:
-
We declare three 2D arrays:
firstMatrix
andsecondMatrix
to store the input matrices, andresultMatrix
to store the product. - The user is prompted to input the dimensions and elements of both matrices. The program checks if the multiplication is possible by ensuring that the number of columns in the first matrix matches the number of rows in the second matrix.
- The result matrix is initialized to zero, and then the multiplication is performed using three nested loops. The innermost loop performs the multiplication and adds the product to the appropriate element in the result matrix.
- Finally, the result matrix is printed.
3. Example Output
Here’s an example of the program in action:
Enter rows and columns of first matrix: 2 3
Enter rows and columns of second matrix: 3 2
Enter elements of the first matrix:
Element at [0][0]: 1
Element at [0][1]: 2
Element at [0][2]: 3
Element at [1][0]: 4
Element at [1][1]: 5
Element at [1][2]: 6
Enter elements of the second matrix:
Element at [0][0]: 7
Element at [0][1]: 8
Element at [1][0]: 9
Element at [1][1]: 10
Element at [2][0]: 11
Element at [2][1]: 12
Resultant Matrix after multiplication:
58 64
139 154
4. Common Mistakes to Avoid
Here are some common pitfalls to watch out for:
- Matrix Dimension Mismatch: Ensure the number of columns in the first matrix matches the number of rows in the second matrix. If they don’t match, matrix multiplication is not possible.
- Array Indexing: Be careful with array indexing, especially when accessing the elements of the matrices during multiplication.
5. Expanding the Program
Once you understand the basics of matrix multiplication, you can extend the program in several ways:
- Write a function to perform matrix multiplication and call it from
main()
. - Handle larger matrices by dynamically allocating memory instead of using fixed-size arrays.
- Modify the program to check for invalid inputs, such as non-integer elements or dimensions that prevent multiplication.
6. Conclusion
In this post, we’ve learned how to write a C program to multiply two matrices. Matrix multiplication is an important operation in many mathematical and scientific applications, and understanding how to implement it helps deepen your understanding of arrays and loops in C.
By practicing matrix multiplication and other array-based problems, you’ll improve your problem-solving skills and become more proficient at handling complex data structures in C programming.
Post a Comment