Add 2D Array In C#

What is 2D Array(Matrix)?

 
Ans:- 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 same dimensions that are, the same number of rows and columns. It is not possible to add a 2 × 3 matrix with a 3 × 2 matrix. 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

  • Declare and initialize 2 two-dimensional arrays X and Y.
  • Calculate the number of rows and columns present in the array X (as 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, add the corresponding elements
        e.g x11 + y11 = sum11
  •  Display the elements of array sum. 
Solution
  1. using System;  
  2.   
  3. namespace ConsoleMultipleClass  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             Program obj = new Program();  
  10.             obj.AddTwoDArray();  
  11.             Console.ReadLine();  
  12.         }  
  13.   
  14.         void AddTwoDArray()  
  15.         {  
  16.             Console.Write("Enter Number to Define Rows & Column:- ");  
  17.             int arrayLength = Convert.ToInt32(Console.ReadLine());  
  18.   
  19.             int[,] array = new int[arrayLength, arrayLength];  
  20.             int[,] arraySecond = new int[arrayLength, arrayLength];  
  21.             int[,] arraySum = new int[arrayLength, arrayLength];  
  22.   
  23.             for (int i = 0; i < arrayLength; i++)  
  24.             {  
  25.                 for (int j = 0; j < arrayLength; j++)  
  26.                 {  
  27.                     Console.Write("Array Index [{0}][{1}]:- ",i,j);  
  28.                     array[i, j] = Convert.ToInt32(Console.ReadLine());  
  29.                 }  
  30.             }  
  31.   
  32.             Console.WriteLine("---------------------------------------------------------------------");  
  33.             Console.WriteLine("This is Your First Array:-");  
  34.   
  35.             for (int i = 0; i < arrayLength; i++)  
  36.             {  
  37.                 for (int j = 0; j < arrayLength; j++)  
  38.                 {  
  39.                     if (j == 0)  
  40.                     {  
  41.                         Console.Write(array[i,j]);  
  42.                     }  
  43.                     else  
  44.                     {  
  45.                         Console.Write(" " + array[i,j]);  
  46.                     }  
  47.                       
  48.                 }  
  49.                 Console.WriteLine();  
  50.             }  
  51.   
  52.             Console.WriteLine("---------------------------------------------------------------------");  
  53.             Console.WriteLine("Now Enter Your Second Array");  
  54.   
  55.             for (int i = 0; i < arrayLength; i++)  
  56.             {  
  57.                 for (int j = 0; j < arrayLength; j++)  
  58.                 {  
  59.                     Console.Write("Array Index [{0}][{1}]:- ", i, j);  
  60.                     arraySecond[i, j] = Convert.ToInt32(Console.ReadLine());  
  61.                 }  
  62.             }  
  63.   
  64.             Console.WriteLine("---------------------------------------------------------------------");  
  65.             Console.WriteLine("This is Your Second Array:-");  
  66.   
  67.             for (int i = 0; i < arrayLength; i++)  
  68.             {  
  69.                 for (int j = 0; j < arrayLength; j++)  
  70.                 {  
  71.                     if (j == 0)  
  72.                     {  
  73.                         Console.Write(arraySecond[i, j]);  
  74.                     }  
  75.                     else  
  76.                     {  
  77.                         Console.Write(" " + arraySecond[i, j]);  
  78.                     }  
  79.   
  80.                 }  
  81.                 Console.WriteLine();  
  82.             }  
  83.   
  84.             Console.WriteLine("---------------------------------------------------------------------");  
  85.             Console.WriteLine("Do you want to add this arrays:- (Y/N)");  
  86.   
  87.             string userInput = Convert.ToString(Console.ReadLine());  
  88.   
  89.             if (userInput.ToUpper() == "Y")  
  90.             {  
  91.                 for (int i = 0; i < arrayLength; i++)  
  92.                 {  
  93.                     for (int j = 0; j < arrayLength; j++)  
  94.                     {  
  95.                         arraySum[i, j] = array[i, j] + arraySecond[i, j];  
  96.                     }  
  97.                 }  
  98.                 Console.WriteLine("---------------------------------------------------------------------");  
  99.                 Console.WriteLine("Array is Added Successfully Here is your Result");  
  100.                 Console.WriteLine("---------------------------------------------------------------------");  
  101.                 for (int i = 0; i < arrayLength; i++)  
  102.                 {  
  103.                     for (int j = 0; j < arrayLength; j++)  
  104.                     {  
  105.                         if (j == 0)  
  106.                         {  
  107.                             Console.Write(arraySum[i,j]);  
  108.                         }  
  109.                         else  
  110.                         {  
  111.                             Console.Write(" " + arraySum[i, j]);  
  112.                         }  
  113.                           
  114.                     }  
  115.                     Console.WriteLine();  
  116.                 }  
  117.             }  
  118.             else  
  119.             {  
  120.                 Console.WriteLine("Program Terminate Press Enter To Exit............");  
  121.             }  
  122.         }  
  123.     }  
  124. }  
Like this article, if you find it helpful.