Advertisement
Guest User

Untitled

a guest
Jun 14th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. #include "header.h"
  2.  
  3. void systemOfLinearEquations::GaussFormula()
  4. {
  5.     for(int i = 0 ; i < numEquations ; ++i)
  6.     {
  7.         double maxElement = abs(supplementMatrix[i][i]);
  8.         int maxRow = i;
  9.  
  10.         for(int j = i + 1 ; j < numEquations ; ++j)
  11.         {
  12.             if(abs(supplementMatrix[j][i]) > maxElement)
  13.             {
  14.                 maxElement = abs(supplementMatrix[j][i]);
  15.                 maxRow = j;
  16.             }
  17.         }
  18.  
  19.         for(int j = i ; j < numEquations + 1 ; ++j)
  20.         {
  21.             double temp = supplementMatrix[maxRow][j];
  22.  
  23.             supplementMatrix[maxRow][j] = supplementMatrix[i][j];
  24.  
  25.             supplementMatrix[i][j] = temp;
  26.         }
  27.  
  28.         for(int j = i + 1 ; j < numEquations; ++j)
  29.         {
  30.             double multiplier = -supplementMatrix[j][i] / supplementMatrix[i][i];
  31.  
  32.             for(int k = i ; k < numEquations + 1 ; ++k)
  33.             {
  34.                 if (i == k)
  35.                 {
  36.                     supplementMatrix[j][k] = 0;
  37.                 }
  38.  
  39.                 else
  40.                 {
  41.                     supplementMatrix[j][k] += multiplier * supplementMatrix[i][k];
  42.                 }
  43.             }
  44.         }
  45.     }
  46.  
  47.     double* solution;
  48.     allocateArray1D(solution, numEquations);
  49.  
  50.     for(int i = numEquations - 1 ; i >= 0 ; --i)
  51.     {
  52.         solution[i] = supplementMatrix[i][numEquations] / supplementMatrix[i][i];
  53.  
  54.         for(int k = i - 1 ; k >= 0 ; --k)
  55.         {
  56.             supplementMatrix[k][numEquations] -= supplementMatrix[k][i] * solution[i];
  57.         }
  58.     }
  59.  
  60.     systemOfLinearEquations::getDefaultSupplementMatrix();
  61.  
  62.     cout << endl;
  63.  
  64.     for(int i = 0 ; i < numEquations ; ++i)
  65.     {
  66.         cout << "x" << i + 1 << " = " << solution[i]<<endl;
  67.     }
  68.  
  69.     cout << endl;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement