Hello, I have such a problem. I need to write a program that solves SLAU using the Gauss method. The program works for me, but not everything is written, I do not know what needs to be added there. The program should work on the direct course (bringing the expanded matrix to a triangular form) and on the reverse course (finding the unknowns). Help me please. Program text:

#include <stdio.h> #include <conio.h> #include <math.h> #define N 50 void glavelem(int k, double mas[][N + 1], int n, int otv[]); int main(void) { double mas[N][N + 1]; double x[N]; //корни системы int otv[N]; //отвечает за порядок корней int i, j, k, n; //ввод данных //clrscr(); do { printf("введите число уравнений сисемы: "); scanf("%d", &n); if (N < n) printf("слишком большое число уравнений, повторите ввод\n"); } while (N < n); printf("введите систему:n"); for (i = 0; i < n; i++) for (j = 0; j < n + 1; j++) scanf("%lf", &mas[i][j]); //вывод введенной системы //clrscr(); printf("система:n"); for (i = 0; i < n; i++) { for (j = 0; j < n + 1; j++) printf("%7.2f ", mas[i][j]); printf("n"); } //сначало все корни по порядку for (i = 0; i < n + 1; i++) otv[i] = i; //прямой ход метода Гаусса for (k = 0; k < n; k++) { glavelem(k, mas, n, otv); if (fabs(mas[k][k]) < 0.0001) { printf("система не имеет единственного решения"); return (0); } for (j = n; j >= k; j--) mas[k][j] /= mas[k][k]; for (i = k + 1; i < n; i++) for (j = n; j >= k; j--) mas[i][j] -= mas[k][j] * mas[i][k]; } //обратных ход for (i = 0; i < n; i++) x[i] = mas[i][n]; for (i = n - 2; i >= 0; i--) for (j = i + 1; j < n; j++) x[i] -= x[j] * mas[i][j]; //вывод результата printf("Îòâåò:n"); for (i = 0; i < n; i++) for (j = 0; j < n; j++) if (i == otv[j]) { //расставляем корни по порядку printf("%fn", x[j]); break; } return (0); } //---------------------------------------------- // описание функции //---------------------------------------------- void glavelem(int k, double mas[][N + 1], int n, int otv[]) { int i, j, i_max = k, j_max = k; double temp; //ищем максимальный по модулю элемент for (i = k; i < n; i++) for (j = k; j < n; j++) if (fabs(mas[i_max][j_max]) < fabs(mas[i][j])) { i_max = i; j_max = j; } //переставляем строки for (j = k; j < n + 1; j++) { temp = mas[k][j]; mas[k][j] = mas[i_max][j]; mas[i_max][j] = temp; } //переставляем столбцы for (i = 0; i < n; i++) { temp = mas[i][k]; mas[i][k] = mas[i][j_max]; mas[i][j_max] = temp; } //учитываем изменение порядка корней i = otv[k]; otv[k] = otv[j_max]; otv[j_max] = i; getch(); } 
  • When the program has been compiled, it only lets you enter how many uraniums in the system and the system itself, and it shows what kind of matrix it will be, and then everything does nothing - Catalina
  • 15.7x + 6.6y-5.7z + 11.5w = -2.4; 8.8x-6.7y + 5.5z-4.5w = 5.6; 6.3x-5.7y-23.4z + 6.6w = 7.7; 14.3x + 8.7y-15.7z-5.8w = 23.4; Let's say you need to solve just such a post - Catalina


2 answers 2

You have getch () at the end of the glavelem function. those. at each iteration, the program waits for any key to be pressed. This is made visible for debugging. Remove this call from the function. You can put main in front of the line return (0)

    1. It is quite enough to look for the main line with the diagonal element maximal in absolute value, and somewhere I read that it is enough to find it ONCE.
    2. It would be useful during the forward move to simultaneously determine the determinant, which is equal to the product of the diagonal elements at each iteration.
    3. The dimensions of the matrix should be assigned dynamically: first, to save memory; secondly, for the beauty of the syllable.
    4. Strings should not be rearranged elementwise, but addresses.
    5. Do not use global variables for loop variables. You will get confused and get lost. Here, on the forum, there were examples of this and, specifically, with the Gauss method.
    6. How do you write the reverse? I did not understand, in truth:
     for(int i = line - 1; i >= 0; i--)//Начало обратного хода { double s = 0.0; for(int j = line - 1; j > i; j--) s += r[j] * temp[i][j]; r[i] = temp[i][line] - s; }//Конец обратного хода 
    • Thank you very much. - Catalina