What is a 2D Array (Matrix)?
A matrix is a rectangular two-dimensional array of numbers arranged in rows and columns. A matrix with m rows and n columns can be called an m × n matrix. Individual entries in the matrix are called elements and can be represented by a[i,j], which suggests that the element a is present in the ith row and jth column.
Addition of two matrices
Two matrices, X and Y, can be added if and only if they have the exact dimensions that are the same number of rows and columns. Adding a 2 × 3 matrix with a 3 × 2 matrix is impossible. The addition of two matrices can be performed by adding their corresponding elements as
(X+ Y)[i,j]= X[i,j] + Y[i,j]
Algorithm to add two matrices
- Declare and initialize two two-dimensional arrays, X and Y.
- Calculate the number of rows and columns present in the array X (as the dimensions of both the arrays are the same) and store it in variables rows and cols, respectively.
- Declare another array sum with similar dimensions.
- Loop through the arrays X and Y and add the corresponding elements. e.g x11 + y11 = sum11
- Display the elements of the array sum.
C# Solution
Here is the complete program written in C# that adds two matrices. The program declares three arrays, array, arraySecond, and arraySum. The arraySum is the result of the addition.
using System;
namespace ConsoleMultipleClass
{
class Program
{
static void Main(string[] args)
{
Program obj = new Program();
obj.AddTwoDArray();
Console.ReadLine();
}
void AddTwoDArray()
{
Console.Write("Enter Number to Define Rows & Column:- ");
int arrayLength = Convert.ToInt32(Console.ReadLine());
int[,] array = new int[arrayLength, arrayLength];
int[,] arraySecond = new int[arrayLength, arrayLength];
int[,] arraySum = new int[arrayLength, arrayLength];
for (int i = 0; i < arrayLength; i++)
{
for (int j = 0; j < arrayLength; j++)
{
Console.Write("Array Index [{0}][{1}]:- ",i,j);
array[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
Console.WriteLine("---------------------------------------------------------------------");
Console.WriteLine("This is Your First Array:-");
for (int i = 0; i < arrayLength; i++)
{
for (int j = 0; j < arrayLength; j++)
{
if (j == 0)
{
Console.Write(array[i,j]);
}
else
{
Console.Write(" " + array[i,j]);
}
}
Console.WriteLine();
}
Console.WriteLine("---------------------------------------------------------------------");
Console.WriteLine("Now Enter Your Second Array");
for (int i = 0; i < arrayLength; i++)
{
for (int j = 0; j < arrayLength; j++)
{
Console.Write("Array Index [{0}][{1}]:- ", i, j);
arraySecond[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
Console.WriteLine("---------------------------------------------------------------------");
Console.WriteLine("This is Your Second Array:-");
for (int i = 0; i < arrayLength; i++)
{
for (int j = 0; j < arrayLength; j++)
{
if (j == 0)
{
Console.Write(arraySecond[i, j]);
}
else
{
Console.Write(" " + arraySecond[i, j]);
}
}
Console.WriteLine();
}
Console.WriteLine("---------------------------------------------------------------------");
Console.WriteLine("Do you want to add this arrays:- (Y/N)");
string userInput = Convert.ToString(Console.ReadLine());
if (userInput.ToUpper() == "Y")
{
for (int i = 0; i < arrayLength; i++)
{
for (int j = 0; j < arrayLength; j++)
{
arraySum[i, j] = array[i, j] + arraySecond[i, j];
}
}
Console.WriteLine("---------------------------------------------------------------------");
Console.WriteLine("Array is Added Successfully Here is your Result");
Console.WriteLine("---------------------------------------------------------------------");
for (int i = 0; i < arrayLength; i++)
{
for (int j = 0; j < arrayLength; j++)
{
if (j == 0)
{
Console.Write(arraySum[i,j]);
}
else
{
Console.Write(" " + arraySum[i, j]);
}
}
Console.WriteLine();
}
}
else
{
Console.WriteLine("Program Terminate Press Enter To Exit............");
}
}
}
}
Like and share this article if you find it helpful.