There are two DataGridView . It is necessary to make it so that if there is a row in the first matrix, which is all ascending, then it should be copied into the second matrix. I tried to do it like this if (matA[i, j] < matA[i, j+1]) . If executed, it is written to the array. But the fact is that the last column is discarded. So please help with this.

Here is the code that I bring to the array, and not to the matrix for verification. I set columns and rows using numericUpDown.

 rivate void button1_Click(object sender, EventArgs e) { int n = matrA.RowCount; int m = matrA.ColumnCount; int[,] matA = new int[n, m]; int[] mas = new int[m]; int i, j; bool b = false; for (i = 0; i < n; i++) for (j = 0; j < m; j++) matA[i, j] = Convert.ToInt32(matrA[j, i].Value); for (j = 0; j < matrB.ColumnCount; j++) matrB[j, 0].Value = j + 1; i = 1; while (i < n) { for (j = 0; j < m - 1; j++) { if (matA[i, j] < matA[i, j + 1]) { mas[j] = matA[i, j]; int c1 = mas.Length; if (c1 == m) { b = true; } } } i++; } if (b == true) { for (j = 0; j < m; j++) { textBox1.AppendText(mas[j] + "\n"); } } } 
  • one
    can show your code - ikram
  • Wrote the answer, should work. If you have questions - ask, I will try to help :) - Ev_Hyper
  • Thanks, I was able to figure it out - Lornob

1 answer 1

Your algorithm is incorrect. You need to check each row of the matrix separately. It is more convenient here instead of a rectangular array to use an array of arrays, then you can naturally work with the row of the matrix.

To check whether the array elements form an increasing sequence, you can use the following method:

  bool isUpp(int[] arr) { if (arr == null || arr.Length == 1) return false; for (int i = 1; i < arr.Length; i++) if (arr[i] <= arr[i - 1]) return false; return true; } 

Then to select the rows you need from the matrix, it will be enough to write

 var onlyUpp = data.Where(isUpp).ToArray(); 

Example:

  int[][] Fill(int n, int m, int max = 20) { Random rand = new Random(); return Enumerable.Range(0, n) .Select(_ => Enumerable.Range(0, m) .Select(__ => rand.Next(max)) .ToArray()) .ToArray(); } bool isUpp(int[] arr) { if (arr == null || arr.Length == 1) return false; for (int i = 1; i < arr.Length; i++) if (arr[i] <= arr[i - 1]) return false; return true; } private void button1_Click(object sender, EventArgs e) { int n = (int)numericUpDown1.Value; int m = (int)numericUpDown2.Value; if (n < 1 || m < 1) { MessageBox.Show("N, M должны быть >= 2; "); return; } var data = Fill(n, m); var onlyUpp = data.Where(isUpp).ToArray(); dataGridView1.Rows.Clear(); dataGridView1.RowCount = n; dataGridView1.ColumnCount = m; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) dataGridView1[j, i].Value = data[i][j]; dataGridView2.Rows.Clear(); int k = onlyUpp.Length; if (k == 0) { MessageBox.Show("Строка образующую возрастающую последовательность не обнаружена"); return; } dataGridView2.RowCount = k; dataGridView2.ColumnCount = m; for (int i = 0; i < k; i++) for (int j = 0; j < m; j++) dataGridView2[j, i].Value = onlyUpp[i][j]; }