Matrix Multiplication in C for Absolute Beginners
Introduction
In this post, we will learn how to multiply two matrices in C programming. Matrix multiplication is an important concept in mathematics and computer science, used in data analysis, machine learning, and various other fields. We’ll break down the code step-by-step, making it easy for beginners to understand.
Understanding Matrix Multiplication
Matrix multiplication differs from simple element-wise addition. For matrix multiplication:
- If we have two matrices, Matrix A (of dimensions
m x n
) and Matrix B (of dimensionsn x p
), we can multiply them to get a resultant Matrix C (of dimensionsm x p
). - Each element in the resultant matrix is obtained by taking the dot product of the rows of Matrix A and the columns of Matrix B.
Here’s an example to visualize it:
Matrix A: Matrix B: Result Matrix C:
2 4 1 2 3 (2*1 + 4*4) (2*2 + 4*5) (2*3 + 4*6)
3 5 4 5 6 (3*1 + 5*4) (3*2 + 5*5) (3*3 + 5*6)
Steps for Matrix Multiplication
- Ensure the number of columns in Matrix A is equal to the number of rows in Matrix B.
- For each element in the result matrix, take the corresponding row from Matrix A and the column from Matrix B, multiply each pair of elements, and sum them.
Let’s see how to write this in a C program.
Matrix Multiplication Code in C
#include <stdio.h>
int main() {
int rowsA, colsA, rowsB, colsB;
// Step 1: Get matrix dimensions
printf("Enter the number of rows and columns of Matrix A: ");
scanf("%d %d", &rowsA, &colsA);
printf("Enter the number of rows and columns of Matrix B: ");
scanf("%d %d", &rowsB, &colsB);
// Check if multiplication is possible
if (colsA != rowsB) {
printf("Matrix multiplication not possible. Columns of A must equal rows of B.\n");
return 0;
}
int matrixA[rowsA][colsA], matrixB[rowsB][colsB], result[rowsA][colsB];
// Initialize result matrix to zero
for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsB; j++) {
result[i][j] = 0;
}
}
// Step 2: Input elements for Matrix A
printf("Enter elements of Matrix A:\n");
for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsA; j++) {
printf("Enter element at position (%d, %d): ", i + 1, j + 1);
scanf("%d", &matrixA[i][j]);
}
}
// Step 3: Input elements for Matrix B
printf("Enter elements of Matrix B:\n");
for (int i = 0; i < rowsB; i++) {
for (int j = 0; j < colsB; j++) {
printf("Enter element at position (%d, %d): ", i + 1, j + 1);
scanf("%d", &matrixB[i][j]);
}
}
// Step 4: Multiply matrices
for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsB; j++) {
for (int k = 0; k < colsA; k++) {
result[i][j] += matrixA[i][k] * matrixB[k][j];
}
}
}
// Step 5: Display the result matrix
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 of Code, Line by Line
Let’s go through each part of this program to understand how it works.
1. Including the Standard I/O Library
#include <stdio.h>
- This line includes the standard input-output library, allowing us to use functions like
printf
andscanf
.
2. Declaring main
Function
int main() {
- Here, we start the
main
function, where the program’s execution begins.
3. Declaring Variables for Rows and Columns
int rowsA, colsA, rowsB, colsB;
- We declare variables to store the number of rows and columns for both matrices.
4. Taking Matrix Dimensions from the User
printf("Enter the number of rows and columns of Matrix A: ");
scanf("%d %d", &rowsA, &colsA);
printf("Enter the number of rows and columns of Matrix B: ");
scanf("%d %d", &rowsB, &colsB);
- We ask the user to input the dimensions of Matrix A and Matrix B.
5. Check if Multiplication is Possible
if (colsA != rowsB) {
printf("Matrix multiplication not possible. Columns of A must equal rows of B.\n");
return 0;
}
- We check if the number of columns in Matrix A is equal to the number of rows in Matrix B. If not, matrix multiplication cannot be done, and we end the program.
6. Declaring Matrices
int matrixA[rowsA][colsA], matrixB[rowsB][colsB], result[rowsA][colsB];
- We declare arrays for Matrix A, Matrix B, and the resultant matrix.
7. Initializing the Result Matrix to Zero
for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsB; j++) {
result[i][j] = 0;
}
}
- We initialize all elements in the result matrix to zero since we will add values to it during multiplication.
8. Taking Input for Matrix A
printf("Enter elements of Matrix A:\n");
for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsA; j++) {
printf("Enter element at position (%d, %d): ", i + 1, j + 1);
scanf("%d", &matrixA[i][j]);
}
}
- We take input for each element of Matrix A from the user.
9. Taking Input for Matrix B
printf("Enter elements of Matrix B:\n");
for (int i = 0; i < rowsB; i++) {
for (int j = 0; j < colsB; j++) {
printf("Enter element at position (%d, %d): ", i + 1, j + 1);
scanf("%d", &matrixB[i][j]);
}
}
- This block is similar to Step 8 but for Matrix B.
10. Performing Matrix Multiplication
for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsB; j++) {
for (int k = 0; k < colsA; k++) {
result[i][j] += matrixA[i][k] * matrixB[k][j];
}
}
}
- This is the main part of the code where we perform matrix multiplication.
- For each element in the result matrix, we multiply elements in a row from Matrix A with elements in a column from Matrix B, and then sum them up.
11. Displaying the Result Matrix
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");
}
- Finally, we print the resultant matrix by iterating through each row and column of the
result
matrix.
12. Ending the Program
return 0;
}
- This
return 0;
statement signals that the program finished successfully.
Example Output
Suppose the user enters the following matrices:
Matrix A: Matrix B:
2 3 1 2
4 5 3 4
The result after multiplication will be:
Resultant Matrix:
11 16
19 28
Conclusion
Matrix multiplication in C may look complex initially, but with a structured approach, it becomes easier to implement. This program introduces the concept of nested loops and array manipulation, making it ideal for beginners interested in learning about matrix
operations.
Feel free to modify and try with larger matrices! Happy coding!