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();
}
}
}