C# - Matrix - Find sum of 2 matrix


C# - Matrix - Find sum of 2 matrix

CODE
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FinalProjectTemplate { class Program { public static void Main(string[] args) { Console.Write("Enter the number of rows of the matrix: "); int rows = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter the number of columns of the matrix: "); int cols = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Matrix 1..."); int[,] matrixA = readMatrix(rows, cols); Console.WriteLine("Matrix 2..."); int[,] matrixB = readMatrix(rows, cols); int[,] sumMatrix = new int[rows, cols]; //Calculate the sum for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { sumMatrix[i, j] = matrixA[i, j] + matrixB[i, j]; } } Console.WriteLine("Sum Matrix"); printMatrix(sumMatrix, rows, cols); Console.ReadKey(); } private 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("{0}\t", matrix[i, j]); } Console.WriteLine(""); } } private static int[,] readMatrix(int rows, int cols) { int[,] matrix = new int[rows, cols]; Console.Write("Enter the elements in the matrix...\n"); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { Console.Write("[{0}],[{1}] : ", i, j); matrix[i, j] = Convert.ToInt32(Console.ReadLine()); } } return matrix; } } }

Comments