Advertisement
Vladislav_Bezruk

Solve SLAR

Sep 29th, 2021
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. double** readMatrix(int n, char c) {
  7.     double** matrix = new double*[n];
  8.    
  9.     for (int i = 0; i < n; i++) {
  10.         matrix[i] = new double[n];
  11.     }
  12.    
  13.     cout << endl << "Enter matrix " << c << " [" << n << "x" << n << "]:" << endl;
  14.    
  15.     for (int i = 0; i < n; i++) {
  16.         for (int j = 0; j < n; j++) {
  17.             cin >> matrix[i][j];
  18.         }
  19.     }
  20.    
  21.     return matrix;
  22. }
  23.  
  24. double* readArray(int n, char c) {
  25.     double* array = new double[n];
  26.    
  27.     cout << endl << "Enter array " << c << " [" << n << "]:" << endl;
  28.    
  29.     for (int i = 0; i < n; i++) {
  30.         cin >> array[i];
  31.     }
  32.    
  33.     return array;
  34. }
  35.  
  36. void solve(double** A, double* B, int n, double EPS) {
  37.     double prevX[n] = { 0. }, X[n] = { 0. }, d;
  38.     int i = 0;
  39.    
  40.     cout << endl;
  41.    
  42.     do {
  43.         cout << "Iteration :: " << i << "\t   >>\tx = {";
  44.        
  45.         for (int i = 0; i < n; i++) {
  46.             prevX[i] = X[i];
  47.            
  48.             if (fabs(X[i]) > EPS) {
  49.                 cout << X[i];
  50.             } else {
  51.                 cout << 0;
  52.             }
  53.            
  54.             if (i != n - 1) {
  55.                 cout << ", ";
  56.             }
  57.         }
  58.        
  59.         cout << "}";
  60.        
  61.         for (int i = 0; i < n; i++) {
  62.             X[i] = B[i];
  63.            
  64.             for (int j = 0; j < n; j++) {
  65.                 if (i != j) {
  66.                     if (j < i) {
  67.                         X[i] += -1 * A[i][j] * X[j];
  68.                     } else {
  69.                         X[i] += -1 * A[i][j] * prevX[j];
  70.                     }
  71.                 }
  72.             }
  73.            
  74.             X[i] /= A[i][i];       
  75.         }
  76.        
  77.         d = fabs(X[0] - prevX[0]);
  78.        
  79.         for (int i = 1; i < n; i++) {
  80.             if (fabs(X[i] - prevX[i]) > d) {
  81.                 d = fabs(X[i] - prevX[i]);
  82.             }
  83.         }
  84.        
  85.         cout << "\t   >>\td = " << d;
  86.        
  87.         if (d < EPS) {
  88.             cout << " < EPS";
  89.         }
  90.        
  91.         cout << endl;
  92.        
  93.         i++;
  94.     } while(d >= EPS);
  95.    
  96.     cout << endl << "RESULT :: x = {";
  97.    
  98.     for (int i = 0; i < n; i++) {
  99.         cout << X[i];
  100.            
  101.         if (i != n - 1) {
  102.             cout << ", ";
  103.         }
  104.     }
  105.        
  106.     cout << "}" << endl;
  107. }
  108.  
  109. int main() {
  110.    
  111.     double EPS;
  112.     int n;
  113.    
  114.     cout << "Enter n :: ";
  115.    
  116.     cin >> n;
  117.    
  118.     double** A = readMatrix(n, 'A');
  119.    
  120.     double* B = readArray(n, 'B');
  121.    
  122.     cout << endl << "Enter EPS: ";
  123.     cin >> EPS;
  124.    
  125.     solve(A, B, n, EPS);
  126.  
  127.     for (int i = 0; i < n; i++) {
  128.         delete[] A[i];
  129.     }
  130.  
  131.     delete[] A;
  132.     delete[] B;
  133.    
  134.     return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement