Matrix multiplication's a pretty common thing in linear algebra. So, you’ve got two matrices and you wanna multiply ’em using C. This program shows you how to do that by passing matrices to a function. Picture a 2x3 matrix with a 3x2 matrix to get a 2x2 result. Let’s jump into this and see how it works in C.
Prerequisites for the C Program to Multiply Two Matrices
Before you start, ensure you know these concepts:
- Understanding of
functionsand how to passarraysto them. - Using
loopsfor iteration in C. - Knowledge of
stdio.hfor input and output operations. - Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler.
Source Code of C Program to Multiply Two Matrices by Passing Matrix to a Function
Here's the complete code to multiply two matrices by passing them to a function:
#include <stdio.h>
void multiply(int first[][10], int second[][10], int result[][10], int r1, int c1, int c2) {
int i, j, k;
// Initialize the result matrix to zero
for (i = 0; i < r1; i++) {
for (j = 0; j < c2; j++) {
result[i][j] = 0;
}
}
// Multiply the matrices
for (i = 0; i < r1; i++) {
for (j = 0; j < c2; j++) {
for (k = 0; k < c1; k++) {
result[i][j] += first[i][k] * second[k][j];
}
}
}
}
int main() {
int first[10][10], second[10][10], result[10][10];
int r1, c1, r2, c2, i, j;
printf("Enter the number of rows for the first matrix: ");
scanf("%d", &r1);
printf("Enter the number of columns for the first matrix: ");
scanf("%d", &c1);
printf("Enter the number of columns for the second matrix: ");
scanf("%d", &c2);
// Read the first matrix
printf("Enter the elements of the first matrix:\n");
for (i = 0; i < r1; i++) {
for (j = 0; j < c1; j++) {
scanf("%d", &first[i][j]);
}
}
// Read the second matrix
printf("Enter the elements of the second matrix:\n");
for (i = 0; i < c1; i++) {
for (j = 0; j < c2; j++) {
scanf("%d", &second[i][j]);
}
}
// Call the multiply function
multiply(first, second, result, r1, c1, c2);
// Display the result
printf("Resultant matrix:\n");
for (i = 0; i < r1; i++) {
for (j = 0; j < c2; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
}Try this code yourself — free
Run it in the EduSeekho online compiler. No setup needed.
Output of the Multiply Two Matrices Program
When you execute the code, here's the output you'll see for the example input:
Enter the number of rows for the first matrix: 2
Enter the number of columns for the first matrix: 3
Enter the number of columns for the second matrix: 2
Enter the elements of the first matrix:
1 2 3
4 5 6
Enter the elements of the second matrix:
7 8
9 10
11 12
Resultant matrix:
58 64
139 154
Algorithm to Multiply Two Matrices in C
To multiply two matrices, each element of both matrices is multiplied and summed up row by row and column by column. This is the logic the program implements step by step.
- Read the dimensions of both matrices (number of rows and columns).
- Input the elements of the first and second matrices.
- Initialize the result matrix to zero.
- For each row in the first matrix:
- For each column in the second matrix:
- Multiply each element by the corresponding element in the second matrix and add to the result matrix.
- Display the resultant matrix.
flowchart TD
A([Start]) --> B[/Read dimensions of matrices/]
B --> C[/Read first matrix/]
C --> D[/Read second matrix/]
D --> E[Call function to multiply matrices]
E --> F[Initialize result matrix to 0]
F --> G[i = 0]
G --> H{Is i < rows of first matrix?}
H -->|Yes| I[j = 0]
I --> J{Is j < columns of second matrix?}
J -->|Yes| K[k = 0]
K --> L{Is k < columns of first matrix?}
L -->|Yes| M[Multiply and add to result matrix[i][j]]
M --> N[k++]
N --> L
L -->|No| O[j++]
O --> J
J -->|No| P[i++]
P --> H
H -->|No| Q[Print result matrix]
Q --> R([End])How the C Program to Multiply Two Matrices Works
This section breaks down the code into understandable parts.
#include <stdio.h>: Includes the standard input-output library necessary forprintfandscanf.void multiply(...): This function takes matrices and their dimensions as parameters and calculates their product.int result[10][10]: Declares a result matrix to store the product of the two matrices.for (i = 0; i < r1; i++): Loops through each row of the first matrix.for (j = 0; j < c2; j++): Loops through each column of the second matrix.for (k = 0; k < c1; k++): Iterates over elements to perform the multiplication and addition required for matrix multiplication.multiply(first, second, result, r1, c1, c2): Calls the function to multiply the matrices.printf: Outputs the resultant matrix.return 0;: Ends the program successfully.
Quick Quiz — test your understanding
Tap an option to check your answerWhy do you need to initialize the result matrix to zero before multiplication?
How Fast Is the Multiply Two Matrices Program in C?
The efficiency of matrix multiplication is a classic topic in C programming.
Time Complexity: O(n3). This is because we use three nested loops with for (i = 0; i < r1; i++), for (j = 0; j < c2; j++), and for (k = 0; k < c1; k++). Here, n represents the number of elements being processed across the loops. For example, multiplying two 3x3 matrices requires about 27 operations.
Space Complexity: O(1). This is because we only use a fixed number of integer matrices first, second, result, irrespective of their size.
Common mistake: Forgetting to initialize the result matrix to zero before starting multiplication can yield incorrect results due to leftover values in memory.
Key takeaway: Always make sure matrices are compatible for multiplication by checking their dimensions.
FAQs About C Program to Multiply Two Matrices by Passing Matrix to a Function
Here are some common questions about multiplying matrices in C:
Related Programs
If you found this program helpful, check out these related programs:
- C Program to Multiply Two Matrices Using Multi-dimensional Arrays
- C Program to Add Two Matrices Using Multi-dimensional Arrays
- C Program to Find Transpose of a Matrix with Example
- C Program to Find Largest Element in an Array
- C Program to Find Largest Number Using Dynamic Memory Allocation
Conclusion
When you’re working with matrices, always check their dimensions before trying to multiply them. This program is awesome for getting how C deals with 2D arrays and functions. Next up, try modifying it to add two matrices instead. You’ll notice the logic is pretty similar.
For more practice, check out our C language quiz, or run this program in the C online compiler.