SOURCE CODE-
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
class Solver{
public:
void printmat(float mat[][4]);
void RowRed(float mat[][4]);
};
int main()
{
Solver ob;
float mat[3][4] = {{5, -6, -7, 7},
{3, -2, 5, -17},
{2, 4, -3, 29}}; //answer should be {2, 4, -3}
ob.printmat(mat);
ob.RowRed(mat);
}
void Solver:: printmat(float mat[][4]) // Outputs the matrix
{
int p=3;
int q=4;
for (int i=0; i<p; i++) {
for (int j=0; j<q; j++) {
cout << setw(7) << setprecision(4) << mat[i][j] << ” “;
}
cout << endl;
}
cout << endl;
}
void Solver:: RowRed(float mat[][4])
{
const int nrows = 3; // number of rows
const int ncols = 4; // number of columns
int lead = 0;
while (lead < nrows) {
float d, m;
for (int r = 0; r < nrows; r++) { // for each row …
/* calculate divisor and multiplier */
d = mat[lead][lead];
m = mat[r][lead] / mat[lead][lead];
for (int c = 0; c < ncols; c++) { // for each column …
if (r == lead)
mat[r][c] /= d; // make pivot = 1
else
mat[r][c] -= mat[lead][c] * m; // make other = 0
}
}
lead++;
printmat(mat);
}
}
OUTPUT-
