Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- *
- * Convert a Matrix to its Echelon Form (step by step)
- *
- * Solved by : Ariful Islam Shanto
- */
- #include <bits/stdc++.h>
- using namespace std;
- int main()
- {
- int r, c = 1;
- bool boolCS = false;
- int nOP = 0;
- while(true) {
- nOP = 0;
- std::cout << "Enter the number of rows of the matrix (0 to exit) >> ";
- std::cin >> r;
- if(r == 0) return 0;
- std::cout << "Enter the number of columns of the matrix >> ";
- std::cin >> c;
- if(c == 0) {
- std::cout << "WARNING_MESSAGE: Number of columns must be greater than zero." << endl << endl;
- continue;
- }
- int mat[r][c];
- std::cout << "Enter the matrix: " << endl;
- for (int i = 0; i < r; i++) {
- for (int j = 0; j < c; j++) {
- std::cin >> mat[i][j];
- }
- }
- std::cout << endl << endl;
- int mul_i, mul_j, gcd, temp, shiftRowIndex, cs = 0, step = 0;
- for (int i = 0; i < r - 1; i++) {
- // row shifting (if needed)
- boolCS = false; // gives a positive signal if there is a column that shifted
- if (mat[i][i + cs] == 0) {
- shiftRowIndex = -1;
- for (int j = i + 1; j < r; j++) {
- if ( (i + cs < c) && mat[j][i + cs] != 0) {
- shiftRowIndex = j;
- break;
- }
- }
- if (shiftRowIndex != -1) {
- std::cout << "STEP-" << ++step << endl;
- std::cout << "------------------------" << endl;
- for (int j = 0; j + cs < c; j++) {
- temp = mat[i][j + cs];
- mat[i][j + cs] = mat[shiftRowIndex][j + cs];
- mat[shiftRowIndex][j + cs] = temp;
- }
- // printing column shifting operation
- std::cout << "R" << i+1 << " <==> " << "R" << shiftRowIndex+1 << endl;
- std::cout << "------------------------" << endl;
- // printing the matrix
- for(int x = 0; x < r; x++) {
- for(int y = 0; y < c; y++) {
- std::cout << mat[x][y] << " ";
- }
- std::cout << endl << endl;
- }
- std::cout << "STEP-" << ++step << endl;
- std::cout << "------------------------" << endl;
- }
- else {
- // column shifting
- cs++; // cs = number of shifted columns
- boolCS = true;
- }
- }
- // conditional break for column shifting
- if(nOP + cs >= c) break;
- if(boolCS) {
- i--;
- continue;
- }
- nOP++; // nOP = number of operations
- // printing output window decoration
- std::cout << "STEP-" << ++step << endl;
- std::cout << "------------------------" << endl;
- for (int j = i + 1; j < r; j++) {
- // calculating multiplier for the selected rows
- gcd = std::__gcd(mat[i][i + cs], mat[j][i + cs]);
- gcd = abs(gcd);
- mul_j = mat[i][i + cs] / gcd;
- mul_i = mat[j][i + cs] / gcd;
- // special case: (not needed though but doing this is better as this steps keeps the terms lower)
- if (mul_i == 0) continue;
- // special case: (needed)
- if (mul_i < 0 && mul_j < 0) {
- mul_i *= (-1);
- mul_j *= (-1);
- }
- // printing each operation of each step
- std::cout << "R" << j + 1 << " <-- (" << mul_j << ")*R" << j + 1 << " -(" << mul_i << ")*R" << i + 1 << endl;
- // the operation
- for (int k = i; k + cs < c; k++) {
- mat[j][k + cs] = mul_j * mat[j][k + cs] - mul_i * mat[i][k + cs];
- }
- }
- // printing the matrix in each step
- cout << "------------------------" << endl;
- for (int j = 0; j < r; j++) {
- for (int k = 0; k < c; k++) {
- std::cout << mat[j][k] << " ";
- }
- std::cout << endl;
- }
- std::cout << endl;
- }
- std::cout << "Your Desired Matrix is : " << endl;
- for(int x = 0; x < r; x++) {
- for(int y = 0; y < c; y++) {
- std::cout << mat[x][y] << " ";
- }
- std::cout << endl;
- }
- std::cout << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment