I have to find determinant of order n using Gauss elimination method. I wrote this code:
// Determinant gauss decomposition
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
//Taking input from user
int m, dim;
cout << " Please enter the dimension of determinant and the coefficients" << endl;
cin >> dim;
cout << "Dimension =" << dim << endl;
float n, det = 1.0, given[dim][dim], trans[dim][dim], temp[1][dim];
// "given" is for given matrix and "trans" is for transformed matrix
// "temp" is temporary matrix used for storing a row temporarily
for (int i = 0; i < dim; i++) {
for (int j = 0; j < dim; j++) {
cout << " a" << i << j << " = ";
cin >> given[i][j];
}
}
cout << endl
<< "The input matrix is:" << endl;
for (int i = 0; i < dim; i++) {
cout << endl;
for (int j = 0; j < dim; j++)
cout << given[i][j] << " ";
}
// first transformation
// srow is the row which has to be subtracted.
for (int srow = 0; srow < dim; srow++) {
if (srow < dim - 1) {
cout << endl
<< " after " << srow + 1 << " transformation: the transformed elements are " << endl;
// This shows matrix after each transformation.
}
for (int row = 1; row < dim - srow; row++) {
cout << endl;
for (int col = 0; col < dim; col++) {
if (given[srow][srow] != 0) //new
{
trans[row + srow][col] = given[srow + row][col] - given[srow][col] * (given[row + srow][srow] / given[srow][srow]);
}
else {
cout << " after changing row, ";
for (int col = 0; col < dim; col++) {
temp[1][col] = given[srow][col];
given[srow][col] = -1 * given[srow + 1][col];
given[srow + 1][col] = temp[1][col];
cout << " given[" << srow << "][" << col << "] = "
<< given[srow][col];
}
goto fmat;
}
}
}
// showing the transformed elements after each transformation
for (int p = 1; p < dim - srow; p++) {
for (int q = 0; q < dim; q++) {
given[p + srow][q] = trans[p + srow][q];
cout << " given[" << p + srow << "][" << q << "] = " << given[p + srow][q];
}
cout << endl;
}
// After each transformation, the transformed matrix is taken as the
//given matrix.
if (srow < dim - 1) {
cout << endl
<< " after " << srow + 1 << " transformation, the given matrix transforms to " << endl;
}
cout << endl;
for (int p = 0; p < dim; p++) {
for (int q = 0; q < dim; q++) {
cout << " given[" << p << "][" << q << "] = " << given[p][q];
}
cout << endl;
}
}
// Final transformed upper triangular matrix
fmat:
cout << endl
<< " The final transformed matrix : " << endl;
for (int i = 0; i < dim; i++) {
cout << endl;
for (int j = 0; j < dim; j++)
cout << given[i][j] << " ";
}
cout << endl;
// determinant calculation
for (int i = 0; i < dim; i++) {
det = det * given[i][i];
}
cout << "determinant = " << det;
return 0;
}
Here is the output I got:
Please enter the dimension of determinant and the coefficients
3
Dimension =3
a00 = 1
a01 = 2
a02 = 3
a10 = 2
a11 = 3
a12 = 1
a20 = 3
a21 = 1
a22 = 2
The input matrix is:
1 2 3
2 3 1
3 1 2
after 1 transformation: the transformed elements are
given[1][0] = 0 given[1][1] = -1 given[1][2] = -5
given[2][0] = 0 given[2][1] = -5 given[2][2] = -7
after 1 transformation, the given matrix transforms to
given[0][0] = 1 given[0][1] = 2 given[0][2] = 3
given[1][0] = 0 given[1][1] = -1 given[1][2] = -5
given[2][0] = 0 given[2][1] = -5 given[2][2] = -7
after 2 transformation: the transformed elements are
given[2][0] = 0 given[2][1] = 0 given[2][2] = 18
after 2 transformation, the given matrix transforms to
given[0][0] = 1 given[0][1] = 2 given[0][2] = 3
given[1][0] = 0 given[1][1] = -1 given[1][2] = -5
given[2][0] = 0 given[2][1] = 0 given[2][2] = 18
given[0][0] = 1 given[0][1] = 2 given[0][2] = 3
given[1][0] = 0 given[1][1] = -1 given[1][2] = -5
given[2][0] = 0 given[2][1] = 0 given[2][2] = 18
The final transformed matrix :
1 2 3
0 -1 -5
0 0 18
determinant = -18
Process returned 0 (0x0) execution time : 13.801 s
Press any key to continue.
The problem is: I am getting the transformed matrix twice after 2nd transformation in the output. Can you please help me to improve the code so that it shows the concerned transformed matrix only once?