Task: Enter a two-dimensional integer ragged array and remove positive lines from it. Problem: at a certain point, the index pops out of the array. I can not understand how to fix it.

class Program { static int[][] MatrixInput(int n) { int m; int[][] arr = new int[n][]; for (int i = 0; i < n; i++) { Console.Write("Количество элементов в {0} строке: ", i + 1); m = int.Parse(Console.ReadLine()); arr[i] = new int[m]; } Console.Write("Введите элементы: \n"); for (int i = 0; i < n; ++i) for (int j = 0; j < arr[i].Length; ++j) { Console.Write("a[{0},{1}]= ", i, j); arr[i][j] = int.Parse(Console.ReadLine()); } return arr; } static void MatrixOutput(int[][] arr, int n) { for (int i = 0; i < n; i++) for (int j = 0; j < arr[i].Length; ++j) // Ошибка здесь Console.Write("{0,5} ", arr[i][j]); } static bool DeleteRow(int[] x) { foreach (int value in x) { if (value >= 0) { return false; } } return true; } static void Main(string[] args) { Console.WriteLine("Enter count of arrays"); int n = int.Parse(Console.ReadLine()); Console.WriteLine("Enter the elements of matrix: "); int[][] array = MatrixInput(n); int[] x = new int[n]; int newSize = 0; foreach (int[] row in array) { if (DeleteRow(row)) { newSize++; } } int[][] output = new int[newSize][]; int rowCounter = 0; for (int i = 0; i < array.Length; i++) { if (DeleteRow(array[i])) { output[rowCounter] = array[i]; rowCounter++; } } Console.WriteLine("Matrix after removing: "); MatrixOutput(output, n); } } 

}

Closed due to the fact that Nicolas Chabanovsky is off topic by Mar 1 '18 at 19:47 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Nicolas Chabanovsky
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • And the debugger looked where and which index goes beyond the boundaries? - Vladimir Martyanov
  • one
    You suggest us to guess where exactly the problem occurs in your code? - tym32167
  • @ Vladimir Martiyanov , The commentary states "Error here" - Bigniggabos s
  • @Bigniggaboss I do not ask where the error is. I ask, did you watch the debugger? - Vladimir Martyanov
  • one
    @Bigniggaboss master the debugger and further, it just serves for finding errors in programs. - Vladimir Martyanov

1 answer 1

 if (!DeleteRow(row)) { newSize++; } MatrixOutput(output, output.Length);