The program does not produce errors, but only hangs by clicking on the button, here is the listing of the button.

void __fastcall TForm2::Button3Click(TObject * Sender) { float **m = new float *[N]; float *s; for (int i = 0; i < N; i++) { m[i] = new float[N + 1]; } int i = 0, j = 0, k = 0; try { for (i = 0; i < N; i++) { for (j = 0; j < N + 1; j++) { m[i][j] = StrToFloat(Form2->StringGrid1->Cells[j + 1][i + 1]); } } k = 1; //Прямой проход (сверху вниз) for (i = 0; i < N - 1; i++) { //Поиск строки с ненулевым первым элементом for (j = i; j < N; j++) { if (m[j][i] != 0) { break; } } if (j == N) { throw Exception("Вырожденная система с рангом " + IntToStr(i)); } if (j != i) { //Меняем строки s = m[i]; m[i] = m[j]; m[j] = s; } //Вычитание строк for (j = i + 1; j < N; j++) { if (m[j][i] == 0) { //Уже нулевой элемент continue; } float r = -m[j][i] / m[i][i]; //Обнуляем начало строки for (int k = 0; k < i + 1; k++) { m[j][k] = 0; } //вычитаем окончание строки for (int k = i + 1; k < N + 1; k++) { m[j][k] += m[i][k] * r; } } } //Обратный проход(снизу вверх) for (i = N - 1; i >= 0; i--) { //Диагональный элемент делаем 1. m[i][N] = m[i][N] / m[i][i]; m[i][i] = 1; for (j = 0; j < i; j++) { float r = -m[j][i] / 1; //Вычитание строк m[j][i] = 0; m[j][N] += m[i][N] * r; } for (i = 0; i < N; i++) { Form2->StringGrid1->Cells[i + 1][N + 1] = FloatToStrF(m[i][N], ffGeneral, 7, 4); } } } catch(Exception & e) { if (k == 0) { ShowMessage("Ошибка при вводе элемента [" + IntToStr(i + 1) + "][" + IntToStr(j + 1) + "]." + (e.Message).c_str()); //"Гаусс",MB_OK | MB_ICONERROR); Form2->StringGrid1->Col = j + 1; Form2->StringGrid1->Row = i + 1; } else { ShowMessage("Ошибка при решении системы."); // (e.Message).c_str()); //"Гаусс",MB_OK | MB_ICONERROR); } for (int i = 0; i < N; i++) { delete[]m[i]; } delete[]m; }} 
  • Can you tell what this piece of code is doing? Or is there a mistake in it and need to find it? Then you must provide an error message. - BlackOverlord
  • Well, he will not be able to provide messages, judging by the question. But the fact that the initial information is not enough, can be seen immediately. - alexlz
  • no, but how do you like it ?? “It hangs somewhere on this section ...” have you ever heard of the K & R style ??? - sudo97
  • @ Ilya Mikhnevich, have you ever heard of obfuscators? Yes, the style is nonsense, formatting is not long, but the lack of information is another matter. Under what conditions does it hang? Was it really "Error when entering the item"? Etc. - alexlz
  • 2
    On an infinite loop in this fragment does not look like. Borders and indexes inside IMHO cycles do not change. I can advise the author to "throw a few prints" (actually into a file) between external cycles and see this. - avp

1 answer 1

I noticed only thanks to the comments of the distinguished @avp . Indices change in reverse (course) when recording is made to the Grid. And all because of the fact that the variable cycles are not local in the cycle. In general, the code must be changed - poorly written.

  • Yes you are right. Everywhere in for ... Form2-> StringGrid1 -> ... the author needs to change everything i for example j . - avp 4:19 pm