Please help determine the line that contains the maximum element of the diagonal, or at least stick your nose at the error I admit. I spend most of my time at work. I always delete the last line.

namespace App7 { class Program { public static int MaxDiagonalElement(int[,] matrix) { int max = int.MinValue; int n = (int)Math.Sqrt(matrix.Length); for (int i = 0; i < n; i++) { if (matrix[i, i] > max) { max = matrix[i, i]; } } return max; } static void Main(string[] args) { int[,] B = new int[5, 5]; int[,] C = new int[6, 6]; int[,] B1 = new int[4, 5]; int[,] C1 = new int[5, 6]; int n = 0, MaxElement, index = 0; Random Rnd = new Random(); Console.WriteLine("Matrix B"); Console.WriteLine(); for (int i = 0; i < B.GetLength(0); i++) { for (int k = 0; k < B.GetLength(1); k++) { B[i, k] = Rnd.Next(20); Console.Write(B[i, k] + "\t "); } Console.WriteLine(); } // Console.WriteLine(); for (int i = 0; i < 5; i++) { for (int k = 0; k < 5; k++) { } if (MaxDiagonalElement(B) == MaxDiagonalElement(B)) n = i; } for (int i = 0; i < 5; i++) { if (i == n) continue; for (int k = 0; k < 5; k++) { B[index, k] = B[i, k]; } index++; } for (int i = 0; i < 4; i++) { for (int k = 0; k < 5; k++) { Console.Write(B[i, k] + "\t "); } Console.WriteLine(); } Console.WriteLine(); Console.WriteLine("Matrix C"); Console.WriteLine(); /* for (int i = 0; i < C.GetLength(0); i++) { for (int k = 0; k < C.GetLength(1); k++) { C[i, k] = Rnd.Next(20); Console.Write(C[i, k] + "\t "); } Console.WriteLine(); }*/ } } } 
  • For a start, why int[,] ? Use normal containers: List<List<int>> . - VladD
  • @VladD, I haven’t learned a similar way of writing yet ( - Sier
  • What does the condition if (MaxDiagonalElement (B) == MaxDiagonalElement (B)) mean? Not like the right one. - VladD
  • one
    The correct code would be: var diag = M.Select ((row, i) => row [i]); var maxVal = diag.Max (); var maxRowIndices = diag.Select ((val, i) => (val == maxVal? i: -1) .Where (i => i> = 0); --- Learn containers. You cannot delete a row from an array. - VladD
  • @VladD, here I wanted to put a condition that the maximum element of the diagonal is found, while remembering the line number. But unfortunately nothing more correct came to mind. - Sier

0