- What is matrix multiplication and when it is possible
- How to multiply two matrices using nested loops
- The condition for matrix multiplication (columns of A = rows of B)
- Step-by-step explanation of the program
- Practice exercises to test your understanding
Introduction
In this tutorial, we will learn how to write a C program to multiply two matrices.
Matrix multiplication is a binary operation that produces a matrix from two matrices. It is a fundamental operation in linear algebra and is used in many applications.
š” Key Point: Matrix multiplication is possible only when the number of columns of the first matrix equals the number of rows of the second matrix.
Matrix Multiplication Rule
- Matrix A: Dimensions
r1 Ć c1 - Matrix B: Dimensions
r2 Ć c2 - Condition:
c1 == r2(columns of A = rows of B) - Result Matrix C: Dimensions
r1 Ć c2
Formula:
C[i][j] = Σ (A[i][k] à B[k][j]) for k = 0 to (c1 - 1)
Each element of the result matrix is the dot product of a row from A and a column from B.
Visual Example
Matrix A (2Ć3):
[1 2 3] [4 5 6]
Matrix B (3Ć2):
[7 8] [9 10] [11 12]
Result C (2Ć2):
[58 64] [139 154]
Calculation: C[0][0] = 1Ć7 + 2Ć9 + 3Ć11 = 7 + 18 + 33 = 58
C[0][1] = 1Ć8 + 2Ć10 + 3Ć12 = 8 + 20 + 36 = 64
C[1][0] = 4Ć7 + 5Ć9 + 6Ć11 = 28 + 45 + 66 = 139
C[1][1] = 4Ć8 + 5Ć10 + 6Ć12 = 32 + 50 + 72 = 154
Matrix multiplication is used in many real-world applications, such as:
- Computer Graphics: Transformations (rotation, scaling, translation)
- Machine Learning: Neural networks and data transformations
- Physics: Quantum mechanics and coordinate transformations
- Economics: Input-output analysis
C Program to Multiply Two Matrices
#include <stdio.h>
int main() {
int r1, c1, r2, c2, i, j, k;
// ====== FIRST MATRIX ======
printf("Enter the number of rows and columns of first matrix: ");
scanf("%d %d", &r1, &c1);
// ====== SECOND MATRIX ======
printf("Enter the number of rows and columns of second matrix: ");
scanf("%d %d", &r2, &c2);
// Check if multiplication is possible
if(c1 != r2) {
printf("\nā Matrix multiplication not possible!\n");
printf("Number of columns of first matrix (%d) must equal number of rows of second matrix (%d).\n", c1, r2);
return 1;
}
// Declare matrices
int A[r1][c1], B[r2][c2], C[r1][c2];
// ====== READ FIRST MATRIX ======
printf("\nEnter %d elements for first matrix:\n", r1 * c1);
for(i = 0; i < r1; i++) {
for(j = 0; j < c1; j++) {
printf("A[%d][%d] = ", i, j);
scanf("%d", &A[i][j]);
}
}
// ====== READ SECOND MATRIX ======
printf("\nEnter %d elements for second matrix:\n", r2 * c2);
for(i = 0; i < r2; i++) {
for(j = 0; j < c2; j++) {
printf("B[%d][%d] = ", i, j);
scanf("%d", &B[i][j]);
}
}
// ====== MATRIX MULTIPLICATION ======
// Initialize result matrix with 0
for(i = 0; i < r1; i++) {
for(j = 0; j < c2; j++) {
C[i][j] = 0;
}
}
// Multiply A and B
for(i = 0; i < r1; i++) {
for(j = 0; j < c2; j++) {
for(k = 0; k < c1; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
// ====== DISPLAY MATRICES ======
printf("\n=== First Matrix (%dĆ%d) ===\n", r1, c1);
for(i = 0; i < r1; i++) {
for(j = 0; j < c1; j++) {
printf("%d\t", A[i][j]);
}
printf("\n");
}
printf("\n=== Second Matrix (%dĆ%d) ===\n", r2, c2);
for(i = 0; i < r2; i++) {
for(j = 0; j < c2; j++) {
printf("%d\t", B[i][j]);
}
printf("\n");
}
printf("\n=== Result Matrix (%dĆ%d) ===\n", r1, c2);
for(i = 0; i < r1; i++) {
for(j = 0; j < c2; j++) {
printf("%d\t", C[i][j]);
}
printf("\n");
}
return 0;
}
Sample Output
Enter the number of rows and columns of first matrix: 2 3 Enter the number of rows and columns of second matrix: 3 2 Enter 6 elements for 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 6 elements for 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 === First Matrix (2Ć3) === 1 2 3 4 5 6 === Second Matrix (3Ć2) === 7 8 9 10 11 12 === Result Matrix (2Ć2) === 58 64 139 154
Square Matrix Example:
Enter the number of rows and columns of first matrix: 3 3 Enter the number of rows and columns of second matrix: 3 3 Enter 9 elements for 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 A[2][0] = 7 A[2][1] = 8 A[2][2] = 9 Enter 9 elements for second matrix: B[0][0] = 9 B[0][1] = 8 B[0][2] = 7 B[1][0] = 6 B[1][1] = 5 B[1][2] = 4 B[2][0] = 3 B[2][1] = 2 B[2][2] = 1 === First Matrix (3Ć3) === 1 2 3 4 5 6 7 8 9 === Second Matrix (3Ć3) === 9 8 7 6 5 4 3 2 1 === Result Matrix (3Ć3) === 30 24 18 84 69 54 138 114 90
When Multiplication is Not Possible:
Enter the number of rows and columns of first matrix: 2 3 Enter the number of rows and columns of second matrix: 4 2 ā Matrix multiplication not possible! Number of columns of first matrix (3) must equal number of rows of second matrix (4).
Program Explanation
Let's break down the code step by step:
- Include Header File:
#include <stdio.h>includes the standard input/output library. - Declare Variables:
int r1, c1;ā dimensions of first matrixint r2, c2;ā dimensions of second matrixint i, j, k;ā loop counters
- Check Multiplication Condition: If
c1 != r2, the matrices cannot be multiplied. Display an error message and exit. - Read Matrices: Reads elements into both matrices.
- Initialize Result Matrix: Sets all elements of
Cto 0. - Matrix Multiplication (Triple Nested Loop):
- Outer Loop (i): Iterates through rows of A
- Middle Loop (j): Iterates through columns of B
- Inner Loop (k): Iterates through the common dimension (columns of A / rows of B)
C[i][j] += A[i][k] * B[k][j]ā calculates the dot product
- Display Results: Prints all three matrices.
- Return:
return 0;indicates successful program execution.
š Note: The order of multiplication matters. A Ć B is not the same as B Ć A in most cases. Matrix multiplication is not commutative.
Algorithm for Matrix Multiplication
Step-by-step algorithm:
- Start
- Read dimensions of first matrix (r1, c1)
- Read dimensions of second matrix (r2, c2)
- If
c1 != r2:- Print "Multiplication not possible"
- Exit
- Read elements of first matrix (A)
- Read elements of second matrix (B)
- Create result matrix C of size r1 Ć c2
- For
i = 0tor1-1:- For
j = 0toc2-1:- Set
C[i][j] = 0 - For
k = 0toc1-1:C[i][j] += A[i][k] * B[k][j]
- Set
- For
- Print the result matrix
- End
Visualizing Matrix Multiplication
For C[i][j], we take the ith row of A and the jth column of B:
A: [a11 a12 a13] B: [b11 b12] C: [c11 c12]
[a21 a22 a23] [b21 b22] [c21 c22]
[b31 b32]
c11 = a11*b11 + a12*b21 + a13*b31
c12 = a11*b12 + a12*b22 + a13*b32
c21 = a21*b11 + a22*b21 + a23*b31
c22 = a21*b12 + a22*b22 + a23*b32
Time and Space Complexity
| Operation | Time Complexity | Space Complexity |
|---|---|---|
| Reading Matrices | O(r1Ćc1 + r2Ćc2) | O(1) |
| Matrix Multiplication | O(r1 Ć c2 Ć c1) | O(1) |
| Overall | O(r1 Ć c1 Ć c2) | O(r1 Ć c2) |
š» Practice Exercise
Challenge 1: Write a program to multiply two matrices using functions (create a function for matrix multiplication).
Challenge 2: Multiply a matrix by a scalar value (multiply every element by a number).
š Click to Show Solution for Challenge 1
#include <stdio.h>
void multiplyMatrices(int A[][10], int B[][10], int C[][10], int r1, int c1, int c2) {
int i, j, k;
for(i = 0; i < r1; i++) {
for(j = 0; j < c2; j++) {
C[i][j] = 0;
for(k = 0; k < c1; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
}
void displayMatrix(int matrix[][10], int rows, int cols) {
int i, j;
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
}
int main() {
int r1, c1, r2, c2;
printf("Enter rows and columns of first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and columns of second matrix: ");
scanf("%d %d", &r2, &c2);
if(c1 != r2) {
printf("Multiplication not possible!\n");
return 1;
}
int A[10][10], B[10][10], C[10][10];
// Read matrices (code omitted for brevity)
// ... read A and B
multiplyMatrices(A, B, C, r1, c1, c2);
printf("\nResult Matrix:\n");
displayMatrix(C, r1, c2);
return 0;
}
Frequently Asked Questions
1. What is the condition for matrix multiplication?
The number of columns of the first matrix must equal the number of rows of the second matrix. If A is m Ć n and B is n Ć p, then multiplication is possible.
2. What is the result of multiplying two matrices?
If A is m Ć n and B is n Ć p, the result matrix C has dimensions m Ć p. Each element C[i][j] is the dot product of row i of A and column j of B.
3. Is matrix multiplication commutative?
No, matrix multiplication is not commutative. A Ć B is not equal to B Ć A in most cases. The order matters.
4. What is the time complexity of matrix multiplication?
The standard matrix multiplication algorithm has a time complexity of O(m Ć n Ć p), where m and p are the dimensions of the result and n is the common dimension.
5. Can I multiply a matrix by a scalar?
Yes, scalar multiplication is different from matrix multiplication. To multiply a matrix by a scalar, you multiply each element of the matrix by the scalar value.
š” Tip: Matrix multiplication is associative (AĆ(BĆC) = (AĆB)ĆC) and distributive (AĆ(B+C) = AĆB + AĆC).