Introduction
Matrix multiplication is a fundamental operation in linear algebra where two matrices are multiplied to produce a third matrix. This operation is crucial in various applications, including computer graphics, machine learning, and scientific computations. Unlike addition and subtraction, matrix multiplication involves a more complex calculation of dot products.
#include <stdio.h>
#define MAX_SIZE 10 // Define maximum size for the matrices
void multiplyMatrices(int a[MAX_SIZE][MAX_SIZE], int b[MAX_SIZE][MAX_SIZE], int result[MAX_SIZE][MAX_SIZE], int rowsA, int colsA, int colsB) {
for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsB; j++) {
result[i][j] = 0; // Initialize the result element
for (int k = 0; k < colsA; k++) {
result[i][j] += a[i][k] * b[k][j]; // Calculate the dot product
}
}
}
}
int main() {
int a[MAX_SIZE][MAX_SIZE], b[MAX_SIZE][MAX_SIZE], result[MAX_SIZE][MAX_SIZE];
int rowsA, colsA, rowsB, colsB;
printf("Enter the number of rows and columns for the first matrix: ");
scanf("%d %d", &rowsA, &colsA);
printf("Enter elements of the first matrix:\n");
for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsA; j++) {
printf("a[%d][%d]: ", i, j);
scanf("%d", &a[i][j]);
}
}
printf("Enter the number of rows and columns for the second matrix (rows must equal cols of first): ");
scanf("%d %d", &rowsB, &colsB);
if (colsA != rowsB) {
printf("Matrix multiplication not possible: columns of the first matrix must equal rows of the second.\n");
return 1; // Exit if matrices cannot be multiplied
}
printf("Enter elements of the second matrix:\n");
for (int i = 0; i < rowsB; i++) {
for (int j = 0; j < colsB; j++) {
printf("b[%d][%d]: ", i, j);
scanf("%d", &b[i][j]);
}
}
multiplyMatrices(a, b, result, rowsA, colsA, colsB);
printf("Resultant matrix after multiplication:\n");
for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsB; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
}
Explanation
- Function Definition:
multiplyMatrices(int a[MAX_SIZE][MAX_SIZE], int b[MAX_SIZE][MAX_SIZE], int result[MAX_SIZE][MAX_SIZE], int rowsA, int colsA, int colsB)
: This function takes two input matrices (a
andb
), an empty matrix (result
) to store the product, and the dimensions of the matrices.
- Initialization:
- The outer loops iterate through each row of matrix
a
and each column of matrixb
.
- The outer loops iterate through each row of matrix
- Dot Product Calculation:
- For each element in the resultant matrix, a third loop calculates the dot product of the respective row of matrix
a
and column of matrixb
.
- For each element in the resultant matrix, a third loop calculates the dot product of the respective row of matrix
- Main Function:
- Prompts the user for the dimensions and elements of the matrices.
- Checks if multiplication is possible (columns of the first matrix must equal rows of the second).
- Calls
multiplyMatrices
to perform the multiplication and prints the result.
Input
When you run the program, it will prompt for the dimensions and elements of both matrices. For example:
Enter the number of rows and columns for the first matrix: 2 3
Enter elements of the first matrix:
a[0][0]: 1
a[0][1]: 2
a[0][2]: 3
a[1][0]: 4
a[1][1]: 5
a[1][2]: 6
Enter the number of rows and columns for the second matrix (rows must equal cols of first): 3 2
Enter elements of the second matrix:
b[0][0]: 7
b[0][1]: 8
b[1][0]: 9
b[1][1]: 10
b[2][0]: 11
b[2][1]: 12
Output
The output will display the resultant matrix after multiplication:
Resultant matrix after multiplication:
58 64
139 154
Conclusion
Matrix multiplication is a crucial operation in various fields of science and engineering. The provided C implementation demonstrates how to multiply two matrices, ensuring that the conditions for multiplication are met. This example highlights the complexity of matrix operations and provides a foundation for further studies in linear algebra and numerical methods.