Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- void inPutCountEquations(int& countEquations);
- void inPutMatrix(const int countEquations, double* matrix[]);
- void inPutAnswer(const int countEquations, double yVector[]);
- void outPutMatrix(const int countEquations, double* matrix[], double yVector[]);
- void outSystemOfLinearEquations(const int countEquations, double* matrix[], double yVector[]);
- void findInColmn(const int countEquations, const double eps, double* matrix[], int& indexStringWithMaxElementInTheBegin, int& maxElementInColumn, int& k);
- void swapRows(const int countEquations, const int indexStringWithMaxElementInTheBegin, const double eps, double* matrix[], int& maxElementInColumn, double yVector[], int& k, int& flag);
- void normalizeRows(const int countEquations, const double eps, double* matrix[], double yVector[], int& k);
- void correctionRows(const int countEquations, double* matrix[]);
- void reverseSubstitutionRows(const int countEquations, double* matrix[], double yVector[], double xVector[], int& k);
- void showMeResult(const int countEquations, double xVector[]);
- void showMeError(const int countEquations, double* matrix[], double xVector[], double yVector[], double yVectorError[]);
- void cleanMemory(const int countEquations, double* matrix[], double* matrixCopy[], double xVector[], double yVector[], double yVectorCopy[], double yVectorError[]);
- int main()
- {
- int countEquations = 0, indexStringWithMaxElementInTheBegin = 0, indexColumnWithMaxElementInTheBegin = 0, k = 0, maxElementInColumn = 0, maxElementInRow = 0, flag = 1;
- const double eps = 1e-10;
- // Ввод количества уравнений
- inPutCountEquations(countEquations);
- double** matrix = new double* [countEquations]; // Левая часть матрицы
- double** matrixCopy = new double* [countEquations]; // Левая часть матрицы
- double* yVector = new double[countEquations]; // Правая часть матрицы
- double* yVectorCopy = new double[countEquations]; // Правая часть матрицы
- double* yVectorError = new double[countEquations]; // Правая часть матрицы для проверки погрешности
- double* xVector = new double[countEquations]; // Тут ответики у нас будут
- // Ввод левой части матрицы
- inPutMatrix(countEquations, matrix);
- // Ввод правой части матрицы
- inPutAnswer(countEquations, yVector);
- // Копирование левой и правой части матрицы для дальнейшего расчёта погрешности
- for (int j = 0; j < countEquations; j++) {
- matrixCopy[j] = new double[countEquations];
- for (int i = 0; i < countEquations; i++) {
- matrixCopy[j][i] = matrix[j][i];
- }
- yVectorCopy[j] = yVector[j];
- }
- // Вывод введеной матрицы
- outPutMatrix(countEquations, matrix, yVector);
- // Вывод полученной СЛАУ
- outSystemOfLinearEquations(countEquations, matrix, yVector);
- for (k = 0; k < countEquations; k++) {
- findInColmn(countEquations, eps, matrix, indexStringWithMaxElementInTheBegin, maxElementInColumn, k);
- swapRows(countEquations, indexStringWithMaxElementInTheBegin, eps, matrix, maxElementInColumn, yVector, k, flag);
- if (!flag)
- return 0;
- normalizeRows(countEquations, eps, matrix, yVector, k);
- }
- correctionRows(countEquations, matrix);
- reverseSubstitutionRows(countEquations, matrix, yVector, xVector, k);
- showMeResult(countEquations, xVector);
- showMeError(countEquations, matrixCopy, xVector, yVectorCopy, yVectorError);
- // Удаление массивов
- cleanMemory(countEquations, matrix, matrixCopy, xVector, yVector, yVectorCopy, yVectorError);
- return 0;
- }
- void inPutCountEquations(int& countEquations) {
- do {
- cout << "Enter count of equations: "; cin >> countEquations;
- } while (countEquations < 1);
- }
- void inPutMatrix(const int countEquations, double* matrix[]) {
- cout << "Enter your matrix: " << endl;
- for (int j = 0; j < countEquations; j++) {
- matrix[j] = new double[countEquations];
- for (int i = 0; i < countEquations; i++) {
- cin >> matrix[j][i];
- }
- }
- }
- void inPutAnswer(const int countEquations, double yVector[]) {
- cout << "Enter your Y-vector: " << endl;
- for (int j = 0; j < countEquations; j++) {
- cin >> yVector[j];
- }
- }
- void outPutMatrix(const int countEquations, double* matrix[], double yVector[]) {
- system("cls");
- cout << "You've entered: " << endl;
- for (int m = 0; m < countEquations; m++) {
- for (int k = 0; k < countEquations; k++)
- cout << matrix[m][k] << " ";
- cout << "| " << yVector[m];
- cout << endl;
- }
- }
- void outSystemOfLinearEquations(const int countEquations, double* matrix[], double yVector[]) {
- cout << endl << "System of linear equations: " << endl;
- for (int j = 0; j < countEquations; j++) {
- for (int i = 0; i < countEquations; i++)
- {
- cout << matrix[j][i] << "*x" << i;
- if (i < countEquations - 1)
- cout << " + ";
- }
- cout << " = " << yVector[j] << endl;
- }
- }
- void findInColmn(const int countEquations, const double eps, double* matrix[], int& indexStringWithMaxElementInTheBegin, int& maxElementInColumn, int& k) {
- // Поиск строки с максимальным a[i][k]
- maxElementInColumn = abs(matrix[k][k]);
- indexStringWithMaxElementInTheBegin = k;
- for (int i = k; i < countEquations; i++)
- {
- if (abs(matrix[i][k]) > maxElementInColumn) {
- maxElementInColumn = abs(matrix[i][k]);
- indexStringWithMaxElementInTheBegin = i;
- }
- }
- }
- void swapRows(const int countEquations, const int indexStringWithMaxElementInTheBegin, const double eps, double* matrix[], int& maxElementInColumn, double yVector[], int& k, int& flag) {
- if (maxElementInColumn < eps){
- // Проверка, есть ли на главной диагонали нулевой элемент
- cout << "The decision can't be obtained because of A[" << indexStringWithMaxElementInTheBegin + 1 << "][" << indexStringWithMaxElementInTheBegin + 1 << "]" << endl << "Non-degenerate matrix" << endl;
- flag = 0;
- return;
- }
- for (int j = 0; j < countEquations; j++) {
- double temp = matrix[k][j];
- matrix[k][j] = matrix[indexStringWithMaxElementInTheBegin][j];
- matrix[indexStringWithMaxElementInTheBegin][j] = temp;
- }
- double tmp = yVector[k];
- yVector[k] = yVector[indexStringWithMaxElementInTheBegin];
- yVector[indexStringWithMaxElementInTheBegin] = tmp;
- }
- void normalizeRows(const int countEquations, const double eps, double* matrix[], double yVector[], int& k) {
- for (int i = k; i < countEquations; i++) {
- double tmp = matrix[i][k];
- if (abs(tmp) < eps) continue; // для нулевого коэффициента пропустить
- for (int j = 0; j < countEquations; j++)
- matrix[i][j] /= tmp;
- yVector[i] /= tmp;
- if (i == k) continue; // уравнение не вычитать само из себя
- for (int j = 0; j < countEquations; j++)
- matrix[i][j] = matrix[i][j] - matrix[k][j];
- yVector[i] = yVector[i] - yVector[k];
- }
- }
- void correctionRows(const int countEquations, double* matrix[]) {
- for (int i = 0; i < countEquations; i++){
- for (int j = 0; j < countEquations; j++) {
- if (j < i)
- matrix[i][j] = 0;
- }
- }
- }
- void reverseSubstitutionRows(const int countEquations, double* matrix[], double yVector[], double xVector[], int& k) {
- for (k = countEquations - 1; k >= 0; k--) {
- xVector[k] = yVector[k];
- for (int i = 0; i < k; i++)
- yVector[i] = yVector[i] - matrix[i][k] * xVector[k];
- }
- }
- void showMeResult(const int countEquations, double xVector[]) {
- cout << endl;
- for (int i = 0; i < countEquations; i++) {
- cout << "x" << i << " = " << xVector[i] << endl;
- }
- cout << endl << "X = (";
- for (int i = 0; i < countEquations; i++) {
- if (i == countEquations - 1)
- cout << xVector[i] << ")" << endl;
- else
- cout << xVector[i] << ", ";
- }
- }
- void showMeError(const int countEquations, double* matrix[], double xVector[], double yVector[], double yVectorError[]) {
- double max = 0;
- for (int i = 0; i < countEquations; i++) {
- yVectorError[i] = 0;
- for (int j = 0; j < countEquations; j++) {
- yVectorError[i] += matrix[i][j] * xVector[j];
- }
- yVectorError[i] = fabs(yVector[i] - yVectorError[i]);
- if (yVectorError[i] > max)
- max = yVectorError[i];
- }
- cout << "Max error: " << max << endl;
- }
- void cleanMemory(const int countEquations, double* matrix[], double* matrixCopy[], double xVector[], double yVector[], double yVectorCopy[], double yVectorError[]) {
- for (int j = 0; j < countEquations; j++) {
- delete[] matrix[j];
- delete[] matrixCopy[j];
- }
- delete[] matrix;
- delete[] xVector;
- delete[] yVector;
- delete[] yVectorCopy;
- delete[] yVectorError;
- }
Add Comment
Please, Sign In to add comment