Matrix multiplication is a fundamental operation in linear algebra with various applications in computer graphics, scientific computing, and machine learning. This article provides a comprehensive guide to writing a C Program to Multiply Two Matrices Using Multi-dimensional Arrays. We will cover three different solutions, each explained with examples and outputs. The article is structured with necessary headings for clarity.
Prerequisites
Before diving into the code, ensure you have the following prerequisites:
- Basic understanding of C programming.
- Familiarity with arrays and loops in C.
- A C compiler installed on your system (e.g., GCC).
Solution 1: Simple Iterative Approach
1.1 Explanation
In this solution, we will use a nested loop to iterate through the elements of the matrices and perform the multiplication. The result will be stored in a third matrix.
1.2 Program
#include <stdio.h>
int main() {
int rows1, cols1, rows2, cols2;
printf("Enter the number of rows and columns for the first matrix: ");
scanf("%d %d", &rows1, &cols1);
printf("Enter the number of rows and columns for the second matrix: ");
scanf("%d %d", &rows2, &cols2);
if (cols1 != rows2) {
printf("Matrix multiplication is not possible with the given dimensions.\n");
return 1;
}
int matrix1[rows1][cols1], matrix2[rows2][cols2], product[rows1][cols2];
printf("Enter elements of the first matrix:\n");
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols1; j++) {
scanf("%d", &matrix1[i][j]);
}
}
printf("Enter elements of the second matrix:\n");
for (int i = 0; i < rows2; i++) {
for (int j = 0; j < cols2; j++) {
scanf("%d", &matrix2[i][j]);
}
}
// Initialize the product matrix to zero
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
product[i][j] = 0;
}
}
// Multiply the matrices
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
for (int k = 0; k < cols1; k++) {
product[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
printf("Product of the matrices:\n");
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
printf("%d ", product[i][j]);
}
printf("\n");
}
return 0;
}
1.3 Output
Enter the number of rows and columns for the first matrix: 2 3
Enter the number of rows and columns for the second matrix: 3 2
Enter elements of the first matrix:
1 2 3
4 5 6
Enter elements of the second matrix:
7 8
9 10
11 12
Product of the matrices:
58 64
139 154
Solution 2: Using Functions for Modularity
2.1 Explanation
In this solution, we will create separate functions for input, multiplication, and output of matrices. This modular approach enhances code readability and reusability.
2.2 Program
#include <stdio.h>
void inputMatrix(int matrix[][10], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
}
}
}
void multiplyMatrices(int matrix1[][10], int matrix2[][10], int product[][10], int rows1, int cols1, int rows2, int cols2) {
// Initialize the product matrix to zero
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
product[i][j] = 0;
}
}
// Multiply the matrices
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
for (int k = 0; k < cols1; k++) {
product[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
}
void printMatrix(int matrix[][10], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
int main() {
int rows1, cols1, rows2, cols2;
printf("Enter the number of rows and columns for the first matrix: ");
scanf("%d %d", &rows1, &cols1);
printf("Enter the number of rows and columns for the second matrix: ");
scanf("%d %d", &rows2, &cols2);
if (cols1 != rows2) {
printf("Matrix multiplication is not possible with the given dimensions.\n");
return 1;
}
int matrix1[10][10], matrix2[10][10], product[10][10];
printf("Enter elements of the first matrix:\n");
inputMatrix(matrix1, rows1, cols1);
printf("Enter elements of the second matrix:\n");
inputMatrix(matrix2, rows2, cols2);
multiplyMatrices(matrix1, matrix2, product, rows1, cols1, rows2, cols2);
printf("Product of the matrices:\n");
printMatrix(product, rows1, cols2);
return 0;
}
Output
Enter the number of rows and columns for the first matrix: 2 3
Enter the number of rows and columns for the second matrix: 3 2
Enter elements of the first matrix:
1 2 3
4 5 6
Enter elements of the second matrix:
7 8
9 10
11 12
Product of the matrices:
58 64
139 154
Solution 3: Using Pointers
3.1 Explanation
In this solution, we will use pointers to dynamically allocate memory for the matrices. This approach provides a more flexible way to handle matrix operations, especially for larger matrices or when the matrix size is determined at runtime.
3.2 Program
#include <stdio.h>
#include <stdlib.h>
void inputMatrix(int **matrix, int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
}
}
}
void multiplyMatrices(int **matrix1, int **matrix2, int **product, int rows1, int cols1, int rows2, int cols2) {
// Initialize the product matrix to zero
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
product[i][j] = 0;
}
}
// Multiply the matrices
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
for (int k = 0; k < cols1; k++) {
product[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
}
void printMatrix(int **matrix, int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
int main() {
int rows1, cols1, rows2, cols2;
printf("Enter the number of rows and columns for the first matrix: ");
scanf("%d %d", &rows1, &cols1);
printf("Enter the number of rows and columns for the second matrix: ");
scanf("%d %d", &rows2, &cols2);
if (cols1 != rows2) {
printf("Matrix multiplication is not possible with the given dimensions.\n");
return 1;
}
int **matrix1 = (int **)malloc(rows1 * sizeof(int *));
int **matrix2 = (int **)malloc(rows2 * sizeof(int *));
int **product = (int **)malloc(rows1 * sizeof(int *));
for (int i = 0; i < rows1; i++) {
matrix1[i] = (int *)malloc(cols1 * sizeof(int));
}
for (int i = 0; i < rows2; i++) {
matrix2[i] = (int *)malloc(cols2 * sizeof(int));
}
for (int i = 0; i < rows1; i++) {
product[i] = (int *)malloc(cols2 * sizeof(int));
}
printf("Enter elements of the first matrix:\n");
inputMatrix(matrix1, rows1, cols1);
printf("Enter elements of the second matrix:\n");
inputMatrix(matrix2, rows2, cols2);
multiplyMatrices(matrix1, matrix2, product, rows1, cols1, rows2, cols2);
printf("Product of the matrices:\n");
printMatrix(product, rows1, cols2);
// Free allocated memory
for (int i = 0; i < rows1; i++) {
free(matrix1[i]);
free(product[i]);
}
for (int i = 0; i < rows2; i++) {
free(matrix2[i]);
}
free(matrix1);
free(matrix2);
free(product);
return 0;
}
Output
Enter the number of rows and columns for the first matrix: 2 3
Enter the number of rows and columns for the second matrix: 3 2
Enter elements of the first matrix:
1 2 3
4 5 6
Enter elements of the second matrix:
7 8
9 10
11 12
Product of the matrices:
58 64
139 154
Conclusion
Matrix multiplication using multi-dimensional arrays in C can be implemented in various ways. The simple iterative approach is straightforward and easy to understand. Using functions for modularity enhances code readability and reusability. Utilizing pointers provides flexibility in handling dynamic memory allocation, making it suitable for larger matrices or when the matrix size is determined at runtime. Understanding these methods will help you efficiently handle matrix operations in C programming.