I work with C# I have the need to convert a list to a multidimensional array
What they send me on the list is the following
int[,] matriz = new int[,] { { 1, 2, 2 }, { 3, 4, 3 }, { 5, 6, 1 } };
But they are sending it as follows
List<List<int>> arr
I need to convert that list to a multidimensional array so that it looks like this
int[,] matriz = new int[,] { { 1, 2, 2 }, { 3, 4, 3 }, { 5, 6, 1 } };
How to convert that list to Array?
What happens is that I solved an exercise in the way that it is a multidimensional array but when it comes to implementing the code I am surprised that it is a list
int sumaDiagonalI = 0;
int sumaDiaganalD = 0;
int[,] matriz = new int[,] { { 1, 2, 2 }, { 3, 4, 3 }, { 5, 6, 1 } };
int cantFilas = matriz.GetLength(0);
int cantColumnas = matriz.GetLength(1);
if (cantFilas != cantColumnas)
{
Console.WriteLine("No es una matriz cuadrada");
return;
}
var indiceMáximo = Math.Min(cantFilas, cantColumnas);
for (var i = 0; i < indiceMáximo; i++)
{
sumaDiagonalI += matriz[i, i];
}
int j = 0;
for (int i = 0; i < cantFilas; i++)
{
j = ((cantFilas - 1) - i);
sumaDiaganalD += matriz[i, j];
}
In this algorithm I add the left and right diagonals