Add the diagonal of the matrix

5

I have a matrix and take the diagonal, but do not add the values that exist, could you help me please? This is my code:

        int i,j, filas=0, columnas=0,sumadiagonal=0;
        Console.WriteLine("filas?");
        filas = Convert.ToInt16(Console.ReadLine());
        Console.WriteLine("columnas?");
        columnas = Convert.ToInt16(Console.ReadLine());
        Random aleatorio = new Random();
        int[,] matriz=new int [filas, columnas];
        for (i = 0; i < matriz.GetLength(0); i++)
        {
            for (j = 0; j < matriz.GetLength(1); j++)
            {
                matriz[i, j] = aleatorio.Next(0, 10);
                //Console.WriteLine("Ingrese numero en la posicion {0},{1}",i.ToString(),j.ToString());
            }
        }

        Console.WriteLine("MATRIZ");
        for (i = 0; i < matriz.GetLength(0); i++)
        {
            for (j = 0; j < matriz.GetLength(1); j++)
            {
                Console.Write(matriz[i,j].ToString()+" ");
            }
            Console.WriteLine();
        }

        Console.WriteLine("Diagonal");
        for (i = 0; i < matriz.GetLength(0); i++)
        {
            for (j = 0; j < matriz.GetLength(1); j++)
            {
                if (i==j)
                {
                    Console.Write(matriz[i, j].ToString()+" ");
                }
            }
            Console.WriteLine();
        }

        Console.WriteLine("La suma de la diagonal es: ");
        for (i = 0; i < matriz.GetLength(0); i++)
        {
            for (j = 0; j < matriz.GetLength(1); j++)
            {
                if (i == j)
                {
                    //sumadiagonal =matriz[i,j] + matriz[i,j];
                    Console.Write(matriz[i, j].ToString() + " ");
                }
            }
            Console.WriteLine();
        }
        Console.ReadLine();
    }
}

}

    
asked by jorge 03.08.2017 в 06:20
source

2 answers

5

If you compare i==j then these will be 0 == 0 , 1 == 1 , 2 == 2 , ... , so the most effective is:

Console.WriteLine("La suma de la diagonal es: ");
for (i = 0; i < matriz.GetLength(0) && i < matriz.GetLength(1); i++)
{
   sumadiagonal += matriz[i,i];
}
Console.WriteLine();
Console.Write(sumadiagonal);
    
answered by 03.08.2017 / 06:35
source
4

The correct answer in case the matrix could or could not be square, that is, if the number of rows is different from the number of columns would be:

Console.WriteLine("La suma de la diagonal es: ");
var indiceMáximo = Math.Min(filas , columnas);
for (var i = 0; i < indiceMáximo; i++)
{
    sumadiagonal += matriz[i, i];
}

Console.Write(sumadiagonal);

You must iterate only to the minimum between the dimensions so that the indexes do not get out of the array as Davlio mentions in your comment.

Or if you want, a way to solve it in a single line:)

Console.WriteLine($"La suma de la diagonal es: {Enumerable.Range(0, Math.Min(filas, columnas)).Sum(x=> matriz[x, x])}"); 
    
answered by 03.08.2017 в 09:25