Matrix multiplication's super important in fields like computer graphics and physics simulations. You'll run into it a lot when dealing with multi-dimensional datasets. Let’s dive into how you can multiply two matrices using multi-dimensional arrays in C. So, let’s say you’ve got two matrices: A (2x3) and B (3x2). When you multiply these, you get a new matrix C (2x2).
What You Need Before Writing the C Program to Multiply Two Matrices
Before you start coding, ensure you have the following:
- Understanding of nested loops
- Familiarity with multidimensional arrays
- The
<stdio.h>header - 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 Using Multi-dimensional Arrays
Here's the complete source code for multiplying two matrices using multi-dimensional arrays in C:
#include <stdio.h>
int main() {
int m, n, p, q, i, j, k;
int a[10][10], b[10][10], c[10][10];
// Input dimensions of matrices
printf("Enter rows and columns for first matrix: ");
scanf("%d %d", &m, &n);
printf("Enter rows and columns for second matrix: ");
scanf("%d %d", &p, &q);
// Check if multiplication is possible
if (n != p) {
printf("Matrix multiplication not possible.\n");
return 0;
}
// Input elements of first matrix
printf("Enter elements of matrix A:\n");
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d", &a[i][j]);
// Input elements of second matrix
printf("Enter elements of matrix B:\n");
for (i = 0; i < p; i++)
for (j = 0; j < q; j++)
scanf("%d", &b[i][j]);
// Initialize matrix C to zero
for (i = 0; i < m; i++)
for (j = 0; j < q; j++)
c[i][j] = 0;
// Multiply matrices
for (i = 0; i < m; i++) {
for (j = 0; j < q; j++) {
for (k = 0; k < n; k++)
c[i][j] += a[i][k] * b[k][j];
}
}
// Display the result
printf("Result matrix C:\n");
for (i = 0; i < m; i++) {
for (j = 0; j < q; j++) {
printf("%d ", c[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
Here's what the output looks like when you multiply two matrices with the given code:
Enter rows and columns for first matrix: 2 3
Enter rows and columns for second matrix: 3 2
Enter elements of matrix A:
1 4 2
3 0 1
Enter elements of matrix B:
1 3
0 2
1 4
Result matrix C:
3 19
4 13
Algorithm to Multiply Two Matrices in C
In matrix multiplication, each element in the resulting matrix is the sum of products of elements from the rows of the first matrix and columns of the second. Here's the algorithm step by step:
- Read integers
m,n,p,q(the dimensions of matrices A and B). - Check if
n != p; output an error if true, as multiplication isn't possible. - Read matrix A of size
m x n. - Read matrix B of size
p x q. - Initialize matrix C of size
m x qto zero. - For each element in C, calculate using the formula:
C[i][j] += A[i][k] * B[k][j]. - Print matrix C.
flowchart TD A([Start]) --> B[/Read matrices A and B dimensions/] B --> C[/Read elements of matrix A/] C --> D[/Read elements of matrix B/] D --> E[Initialize matrix C to zero] E --> F["for (i = 0; i < m; i++)"] F --> G["for (j = 0; j < q; j++)"] G --> H["for (k = 0; k < n; k++)"] H --> I["C[i][j] += A[i][k] * B[k][j]"] I --> H H --> G G --> F F --> J[/Display matrix C/] J --> K([End])
How the C Program to Multiply Two Matrices Works
Let's break down the code to understand each part:
#include <stdio.h>: This header file is needed for input and output functions likeprintfandscanf.int main(): The main function where execution of the program starts.int m, n, p, q, i, j, k;: Declare variables for the dimensions and indices.int a[10][10], b[10][10], c[10][10];: Declare matrices A, B, and C to store up to 10x10 elements.- Check if the multiplication is possible with
if (n != p)and output an error if not. - Input matrices A and B using nested loops to store values.
- Initialize matrix C to zero to store the result.
- Use triple nested
forloops to multiply matrices and update matrix C. C[i][j] += A[i][k] * B[k][j]: Calculate and accumulate the sum of products for each element of C.- Print the resulting matrix C using nested loops.
Quick Quiz — test your understanding
Tap an option to check your answerWhat operation is performed in the innermost loop of this matrix multiplication program?
Time and Space Complexity of the C Program to Multiply Two Matrices
Analyzing the time and space complexity helps you understand how this program scales.
Time complexity: O(m * n * q). Here, m and q are the dimensions of the resulting matrix, and n is the shared dimension of A and B — 2 and 3 for two 2x3 and 3x2 matrices. The triple nested loop with for (i = 0; i < m; i++), for (j = 0; j < q; j++), and for (k = 0; k < n; k++) leads to this complexity. For example, with m = 2, n = 3, q = 2, the loop runs 12 times.
Space complexity: O(m * q). This is for storing the result matrix C, which is m by q — four ints for two 2x3 and 3x2 matrices.
Common mistake:Not checking if matrix multiplication can happen (like, if the first matrix's columns equal the second matrix's rows).
Key takeaway:Always check the matrix dimensions before trying to multiply them to steer clear of errors.
FAQs About Multiplying Two Matrices in C
Here are some common questions students have about matrix multiplication in C.
int array[m][n]; where m and n are the dimensions.Related Programs
If you found this program helpful, check out these related programs:
- C Program to Multiply Two Matrices Using Functions
- 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
Remember to check dimensions before you multiply matrices. This program's a classic example you’ll see a lot. If you get it, a bunch of other problems will be easier. Want more practice? Try tweaking it to handle non-square matrices differently.
Test your C skills with our C programming quiz or run this code in the EduSeekho online compiler.