Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# program to add two matrices
Matrix addition is a fundamental operation in linear algebra where two matrices of the same dimensions are added element by element. In C#, we can implement matrix addition using two-dimensional arrays.
Syntax
To declare a two-dimensional array for matrix operations −
int[,] matrix = new int[rows, columns];
Matrix addition formula for each element −
result[i,j] = matrix1[i,j] + matrix2[i,j];
How Matrix Addition Works
Matrix addition requires both matrices to have the same dimensions. Each element at position (i,j) in the first matrix is added to the corresponding element at position (i,j) in the second matrix.
Using Predefined Matrix Values
This example demonstrates matrix addition with predefined values, making it suitable for inline compilation −
using System;
public class MatrixAddition {
public static void Main() {
int rows = 3, cols = 3;
int[,] matrix1 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int[,] matrix2 = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};
int[,] result = new int[rows, cols];
// Adding matrices
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i, j] = matrix1[i, j] + matrix2[i, j];
}
}
// Display first matrix
Console.WriteLine("First Matrix:");
PrintMatrix(matrix1, rows, cols);
// Display second matrix
Console.WriteLine("Second Matrix:");
PrintMatrix(matrix2, rows, cols);
// Display result
Console.WriteLine("Sum of matrices:");
PrintMatrix(result, rows, cols);
}
static void PrintMatrix(int[,] matrix, int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
Console.Write(matrix[i, j] + "\t");
}
Console.WriteLine();
}
Console.WriteLine();
}
}
The output of the above code is −
First Matrix: 1 2 3 4 5 6 7 8 9 Second Matrix: 9 8 7 6 5 4 3 2 1 Sum of matrices: 10 10 10 10 10 10 10 10 10
Using Different Matrix Dimensions
This example shows matrix addition with rectangular matrices (2x4) −
using System;
public class RectangularMatrixAddition {
public static void Main() {
int rows = 2, cols = 4;
int[,] matrixA = {{1, 2, 3, 4}, {5, 6, 7, 8}};
int[,] matrixB = {{8, 7, 6, 5}, {4, 3, 2, 1}};
int[,] sum = new int[rows, cols];
// Perform matrix addition
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
sum[i, j] = matrixA[i, j] + matrixB[i, j];
}
}
Console.WriteLine("Matrix A (2x4):");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
Console.Write($"{matrixA[i, j],3}");
}
Console.WriteLine();
}
Console.WriteLine("\nMatrix B (2x4):");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
Console.Write($"{matrixB[i, j],3}");
}
Console.WriteLine();
}
Console.WriteLine("\nA + B =");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
Console.Write($"{sum[i, j],3}");
}
Console.WriteLine();
}
}
}
The output of the above code is −
Matrix A (2x4): 1 2 3 4 5 6 7 8 Matrix B (2x4): 8 7 6 5 4 3 2 1 A + B = 9 9 9 9 9 9 9 9
Key Rules for Matrix Addition
Both matrices must have the same dimensions (same number of rows and columns).
Addition is performed element-wise: result[i,j] = matrix1[i,j] + matrix2[i,j].
Matrix addition is commutative: A + B = B + A.
Matrix addition is associative: (A + B) + C = A + (B + C).
Conclusion
Matrix addition in C# is straightforward using two-dimensional arrays and nested loops. The key requirement is that both matrices must have identical dimensions, and the addition is performed element by element to produce the result matrix.
