R
R
Ruslan NoFam2021-06-20 14:33:54
.NET
Ruslan NoFam, 2021-06-20 14:33:54

How to sort matrix elements in dataGridView in ascending order?

Good day!
Required:
Fill array a[n][n] with random numbers in the range [-50; fifty]. The N value is entered through the TextBox component Display the elements of the main diagonal of the array (dataGrigView) in ascending order.
Stuck on ordering elements:

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {

    //показать dataGridView
    label6->Visible = true;
    dataGridView1->Visible = true;
    label5->Visible = true;
    dataGridView2->Visible = true;

    int n = Convert::ToInt32(textBox1->Text);

    dataGridView1->RowCount = n;
    dataGridView1->ColumnCount = n;
    dataGridView2->RowCount = 1;
    dataGridView2->ColumnCount = n;

    //создаю массив
    int** arr = new int* [n];
    for (int i = 0; i < n; i++)
      arr[i] = new int[n];

    //заполняю массив данными
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        arr[i][j] = rand() % 100 - 50;
        dataGridView1->Columns[j] -> HeaderText = "Столбец №" + (j + 1).ToString();
        dataGridView1->Rows[i]->Cells[j]->Value = arr[i][j].ToString();
      }
    }

  
    //вывожу главную диагональ массива
    for (int i = 0; i < n; i++)
    {
      for (int j = 0; j < n; j++)
      {
        if (i == j) {
          dataGridView2->Columns[j]->HeaderText = "Элемент №" + (j + 1).ToString();
          dataGridView2->Rows[0]->Cells[j]->Value = arr[i][j].ToString();
        }
          
      }
    }


    //удаляю массив
    for (int i = 0; i < n; i++)
      delete[] arr[i];
  }
};

Result:
60cf27784b586381728568.jpeg
I draw the main diagonal, but I can't arrange it. Killed all day...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Ananiev, 2021-06-21
@Humon91072

Store the main diagonal in an array. Sort the array and display it in the dataGridView.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question