silentkiler029

convert-matrix-to-echelon-form-(by_Shanto)

Oct 15th, 2020
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.84 KB | None | 0 0
  1. /*
  2.  *
  3.  *                  Convert a Matrix to its Echelon Form (step by step)
  4.  *
  5.  *                          Solved by : Ariful Islam Shanto
  6.  */
  7.  
  8. #include <bits/stdc++.h>
  9. using namespace std;
  10.  
  11. int main()
  12. {
  13.     int r, c = 1;
  14.     bool boolCS = false;
  15.     int nOP = 0;
  16.    
  17.     while(true) {
  18.         nOP = 0;
  19.         std::cout << "Enter the number of rows of the matrix (0 to exit) >> ";
  20.         std::cin >> r;
  21.         if(r == 0) return 0;
  22.         std::cout << "Enter the number of columns of the matrix >> ";
  23.         std::cin >> c;
  24.         if(c == 0) {
  25.             std::cout << "WARNING_MESSAGE: Number of columns must be greater than zero." << endl << endl;
  26.             continue;
  27.         }
  28.        
  29.         int mat[r][c];
  30.         std::cout << "Enter the matrix: " << endl;
  31.         for (int i = 0; i < r; i++) {
  32.             for (int j = 0; j < c; j++) {
  33.                 std::cin >> mat[i][j];
  34.             }
  35.         }
  36.  
  37.         std::cout << endl << endl;
  38.  
  39.         int mul_i, mul_j, gcd, temp, shiftRowIndex, cs = 0, step = 0;
  40.         for (int i = 0; i < r - 1; i++) {
  41.             // row shifting (if needed)
  42.             boolCS = false;     // gives a positive signal if there is a column that shifted
  43.             if (mat[i][i + cs] == 0) {
  44.                 shiftRowIndex = -1;
  45.                 for (int j = i + 1; j < r; j++) {
  46.                     if ( (i + cs < c) && mat[j][i + cs] != 0) {
  47.                         shiftRowIndex = j;
  48.                         break;
  49.                     }
  50.                 }
  51.                 if (shiftRowIndex != -1) {
  52.                     std::cout << "STEP-" << ++step << endl;
  53.                     std::cout << "------------------------" << endl;
  54.                     for (int j = 0; j + cs < c; j++) {
  55.                         temp = mat[i][j + cs];
  56.                         mat[i][j + cs] = mat[shiftRowIndex][j + cs];
  57.                         mat[shiftRowIndex][j + cs] = temp;
  58.                     }
  59.  
  60.             // printing column shifting operation
  61.                     std::cout << "R" << i+1 << " <==> " << "R" << shiftRowIndex+1 << endl;
  62.                     std::cout << "------------------------" << endl;
  63.  
  64.             // printing the matrix
  65.                     for(int x = 0; x < r; x++) {
  66.                         for(int y = 0; y < c; y++) {
  67.                             std::cout << mat[x][y] << " ";
  68.                         }
  69.                         std::cout << endl << endl;
  70.                     }
  71.                     std::cout << "STEP-" << ++step << endl;
  72.                     std::cout << "------------------------" << endl;
  73.                 }
  74.                 else {
  75.                     // column shifting
  76.                     cs++;       // cs = number of shifted columns
  77.                     boolCS = true;
  78.                 }
  79.             }
  80.            
  81.             // conditional break for column shifting
  82.             if(nOP + cs >= c) break;
  83.            
  84.             if(boolCS) {
  85.                 i--;
  86.                 continue;
  87.             }
  88.            
  89.             nOP++;  // nOP = number of operations
  90.            
  91.             // printing output window decoration
  92.             std::cout << "STEP-" << ++step << endl;
  93.             std::cout << "------------------------" << endl;
  94.            
  95.             for (int j = i + 1; j < r; j++) {
  96.         // calculating multiplier for the selected rows
  97.                 gcd = std::__gcd(mat[i][i + cs], mat[j][i + cs]);
  98.                 gcd = abs(gcd);
  99.                 mul_j = mat[i][i + cs] / gcd;
  100.                 mul_i = mat[j][i + cs] / gcd;
  101.  
  102.                 // special case: (not needed though but doing this is better as this steps keeps the terms lower)
  103.                 if (mul_i == 0) continue;
  104.  
  105.                 // special case: (needed)
  106.                 if (mul_i < 0 && mul_j < 0) {
  107.                     mul_i *= (-1);
  108.                     mul_j *= (-1);
  109.                 }
  110.        
  111.         // printing each operation of each step
  112.                 std::cout << "R" << j + 1 << " <-- (" << mul_j << ")*R" << j + 1 << " -(" << mul_i << ")*R" << i + 1 << endl;
  113.         // the operation
  114.                 for (int k = i; k + cs < c; k++) {
  115.                     mat[j][k + cs] = mul_j * mat[j][k + cs] - mul_i * mat[i][k + cs];
  116.                 }
  117.             }
  118.             // printing the matrix in each step
  119.             cout << "------------------------" << endl;
  120.             for (int j = 0; j < r; j++) {
  121.                 for (int k = 0; k < c; k++) {
  122.                     std::cout << mat[j][k] << " ";
  123.                 }
  124.                 std::cout << endl;
  125.             }
  126.             std::cout << endl;
  127.         }
  128.         std::cout << "Your Desired Matrix is : " << endl;
  129.         for(int x = 0; x < r; x++) {
  130.             for(int y = 0; y < c; y++) {
  131.                 std::cout << mat[x][y] << " ";
  132.             }
  133.             std::cout << endl;
  134.         }
  135.         std::cout << endl;
  136.     }
  137.     return 0;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment