Advertisement
skb50bd

Gauss-Jordan Elimination

Jul 22nd, 2017
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #define SIZE 15
  4. using namespace std;
  5.  
  6. float M[SIZE][SIZE];
  7. int vars;
  8.  
  9. void display ( ) {
  10.     for (int i = 0; i < vars; i++) {
  11.         for (int j = 0; j <= vars; j++)
  12.             cout << M[i][j] << "\t";
  13.         cout << endl;
  14.     }
  15. }
  16.  
  17. void gauss ( ) {
  18.     for (int i = 0; i < vars; i++) {
  19.         for (int j = i + 1; j < vars; j++) {
  20.             float banisher = M[j][i] / M[i][i];
  21.             for (int k = i; k <= vars; k++)
  22.                 M[j][k] -= banisher * M[i][k];
  23.         }
  24.     }
  25.     cout << endl << endl;
  26.     cout << "After Basic Gauss Elimination" << endl;
  27.     display ( );
  28.  
  29.     // Normalization
  30.     for (int i = 0; i < vars; i++) {
  31.         float div = M[i][i];
  32.         for (int j = i; j <= vars; j++)
  33.             M[i][j] /= div;
  34.     }
  35. }
  36.  
  37. void jordan ( ) {
  38.     for (int i = vars - 1; i >= 0; i--) {
  39.         for (int j = i - 1; j >= 0; j--) {
  40.             float banisher = M[j][i] / M[i][i];
  41.             for (int k = i; k <= vars; k++)
  42.                 M[j][k] -= banisher * M[i][k];
  43.         }
  44.     }
  45. }
  46.  
  47. int main ( ) {
  48.     // Getting Input
  49.     cout << "Number of Equations: ";
  50.     cin >> vars;
  51.     cout << "Enter the Augmented Matrix" << endl;
  52.     for (int i = 0; i < vars; i++)
  53.         for (int j = 0; j <= vars; j++)
  54.             cin >> M[i][j];
  55.     system ("cls");
  56.     cout << "Your Input" << endl;
  57.     display( );
  58.  
  59.     // Forward Elimination
  60.     gauss( );
  61.     cout << endl << endl;
  62.     cout << "Row Echelon Form" << endl;
  63.     display( );
  64.  
  65.     // Backward Elimination
  66.     jordan( );
  67.     cout << endl << endl;
  68.     cout << "Reduced Row Echelon Form" << endl;
  69.     display( );
  70.  
  71.     cout << endl << endl << endl;
  72.     cout << "RESULT" << endl;
  73.     for (int i = 0; i < vars; i++)
  74.         cout << "X" << i + 1 << " = " << M[i][vars] << endl;
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement