Commenting the code

pull/1273/head
guille 2023-10-15 21:32:16 +02:00
parent 785929c672
commit f37fe352f0
1 changed files with 17 additions and 4 deletions

View File

@ -1,4 +1,7 @@
/*
This problem does the addition the diagonals in a square
matrix and then, it does the difference of both in
absolute value.
Time of complexity: O(n^2)
*/
@ -13,12 +16,18 @@ namespace Algorithms.Maths
int secondDiagonal = 0;
int longitud = Convert.ToInt32(arr.LongCount());
int total = 0;
/**
Loop to iterate over the entire matrix twice. Each iteration adds
the current element to the corresponding diagonal.
**/
for (int i =0; i < longitud; i++){
firstDiagonal += arr[i][i];
secondDiagonal += arr[i][(longitud)-1-i];
}
/*
Difference of both diagonals. The conditional statement will "convert"
the possible negative value into positive.
**/
total = firstDiagonal - secondDiagonal;
if (total < 0) {
total = total * (-1);
@ -34,18 +43,22 @@ namespace Algorithms.Maths
{
public static void Main(string[] args)
{
//Variable to get the number of rows and columns.
int n = Convert.ToInt32(Console.ReadLine().Trim());
//Declaring list of two dimensions
List<List<int>> arr = new List<List<int>>();
//For loop to save user input into the list
for (int i = 0; i < n; i++)
{
arr.Add(Console.ReadLine().TrimEnd().Split(' ').ToList().Select(arrTemp => Convert.ToInt32(arrTemp)).ToList());
}
//Calling the previous method and saving it in result variable
int result = Matrix.diagonalDifference(arr);
//Finally it displays out the result
Console.WriteLine(result);