Matrix addition's a common thing in math and engineering. If you've got two matrices and wanna add them element by element, this program shows you how to do it in C. For instance, if Matrix A is [[1, 2], [3, 4]] while Matrix B is [[5, 6], [7, 8]], the result will be [[6, 8], [10, 12]].
Let's dive into adding two matrices using multi-dimensional arrays.
Prerequisites for Adding Matrices in C
Before writing the matrix addition program in C, make sure you know the following:
- Multi-dimensional arrays and their syntax in C.
- The
<stdio.h>library for input and output. - Nested loops for iterating through matrix elements.
- Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler.
Source Code of C Program to Add Two Matrices
Here's the complete code to add two matrices using multi-dimensional arrays in C.
#include <stdio.h>
int main() {
int a[10][10], b[10][10], c[10][10];
int m, n, i, j;
// Input dimensions
printf("Enter the number of rows (max 10): ");
scanf("%d", &m);
printf("Enter the number of columns (max 10): ");
scanf("%d", &n);
// Input elements of first matrix
printf("Enter elements of matrix A:\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("Enter element A[%d][%d]: ", i, j);
scanf("%d", &a[i][j]);
}
}
// Input elements of second matrix
printf("Enter elements of matrix B:\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("Enter element B[%d][%d]: ", i, j);
scanf("%d", &b[i][j]);
}
}
// Adding matrices
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
c[i][j] = a[i][j] + b[i][j];
}
}
// Displaying the result
printf("Sum of matrices A and B:\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("%d\t", 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 Add Two Matrices Program
Below is a sample run of the program to add two matrices. Enter values for the matrices when prompted.
Enter the number of rows (max 10): 2
Enter the number of columns (max 10): 2
Enter elements of matrix A:
Enter element A[0][0]: 1
Enter element A[0][1]: 2
Enter element A[1][0]: 3
Enter element A[1][1]: 4
Enter elements of matrix B:
Enter element B[0][0]: 5
Enter element B[0][1]: 6
Enter element B[1][0]: 7
Enter element B[1][1]: 8
Sum of matrices A and B:
6 8
10 12
Algorithm to Add Two Matrices in C
Matrix addition involves adding corresponding elements from two matrices. Here is the exact logic the program follows, step by step.
- Read the number of rows and columns for the matrices.
- Input elements for matrix A and matrix B.
- Initialize matrix C to store the sum of matrices A and B.
- For each element, add the corresponding elements from matrix A and B, and store the result in matrix C.
- Output the resulting matrix C.
flowchart TD A([Start]) --> B[/Read matrix A and B/] B --> C["Initialize matrix C as the result"] C --> D["For each row i = 0 to m"] D --> E["For each column j = 0 to n"] E --> F["C[i][j] = A[i][j] + B[i][j]"] F --> E E --> G["Display matrix C"] G --> H([End])
How the C Program to Add Two Matrices Works
Let's break down the code to understand how it adds two matrices.
- The program starts by including the
<stdio.h>library which is needed for input-output functions. - Three 2D arrays
a,b, andcare declared to hold the matrices. - The user is prompted to enter the dimensions of the matrices. These are stored in
mandn. - Elements of matrix A and matrix B are entered using nested loops, displaying prompts like
Enter element A[i][j]. - A nested loop structure iterates over each matrix element to perform addition and stores the result in matrix
c. - Finally, the program outputs the resulting matrix using another set of nested loops.
- The program ends with a
return 0statement, indicating successful execution.
Quick Quiz — test your understanding
Tap an option to check your answerWhat is the main purpose of using multi-dimensional arrays in this program?
How Fast Is the Matrix Addition in C?
Let's discuss the time and space complexity of this matrix addition program.
Time Complexity: O(m * n). Here, m is the number of rows and n is the number of columns in the matrices. The program uses two nested loops: for (i = 0; i < m; i++) and for (j = 0; j < n; j++), each iterating over all elements. For example, a 3x3 matrix will perform 9 operations.
Space Complexity: O(m * n). The program uses three matrices a, b, and c, each with m * n integers. Thus, space grows with the size of the input matrices.
Common mistake:Mixing row and column indices when accessing matrix elements can cause unexpected results.
Key takeaway:Matrix addition using multi-dimensional arrays is simple and efficient, but always make sure that the matrices match in size.
FAQs About Adding Two Matrices in C
Here are some common questions and answers about matrix addition in C.
a, b, and c.<stdio.h> for input and output. No extra libraries are required for basic matrix stuff.c[i][j] = a[i][j] + b[i][j] to subtraction: c[i][j] = a[i][j] - b[i][j].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 Multiply Two Matrices Using Functions
- 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
Matrix addition in C with multi-dimensional arrays is simple once you get the hang of multi-dimensional loops. Try different sizes and values to really understand it. Just remember, both matrices need to be the same size for addition to work.
Ready for more? Test your skills with our C Programming Quiz, or check out your code in the online compiler.