I'm having an issue with the elimination portion of my Gaussian Elimination problem. The program crashes while running, I can't figure out what is causing the wrong rows to switch, and why it is not eliminating correctly.
void Matrix::gaussianElimination()
{
int i, j, max;
for(j = 0; j < N; ++j)
{
i = j;
max = i++;
for(max = i++; max < N; max++)
while(max < N)
{
if (fabs(C[i][j]) >= fabs(C[max][j]))
++max;
else
{
i = max++;
}
}
if(C[max][j] == 0)
{
E = 0;
return;
}
//If p > j: interchange rows p & j
if(max > j)
{
double temp;
for(int m = 0; m <= N; ++m)
{
temp = C[j][m];
C[j][m] = C[max][m];
C[max][m] = temp;
cout.precision(2);
cout << C[j][m] << " switched with " << C[max][m] << endl;
}
}
//For each i > j: row i - (Cij/Cjj)* row j
i = j++;
for(int row_i = i; row_i < N; row_i++)
{
for(int row_j = j; row_j <= N; row_j++)
{
C[row_i][row_j] = C[row_i][row_j] - ((C[i][j]/C[j][j]) * C[j][row_j]);
cout.precision(2);
cout.width(10);
cout << C[row_i][row_j];
}
cout << endl;
}
}
return;
}
//Elimination to create a diagonal
for(i = N; i >= j; --i)
{
for(k = j++; k < N; ++k)
{
C[k][i] -= C[k][j]/C[j][j] * C[j][i];
}
for(k = 0; k < N; ++k)
{
for(i = 0; i <= N; ++i)
{
cout.width(10);
cout.precision(2);
cout.setf(ios::fixed);
cout << C[k][i];
}
cout << endl;
}
}
}
for(i = N--; i >=0; --i)
{
C[i][N] = C[i][N] / C[i][i];
C[i][i] = 1;
for(j = i--; j >= 0; --j)
{
C[j][N] -= C[j][i] * C[i][N];
C[j][i] = 0;
}
}
return;
}
void Matrix::solve_x(const Matrix& aMatrix, const Matrix& bMatrix )
{
int row, col;
A = aMatrix.get_A();
b = bMatrix.get_b();
assert( A != NULL);
assert( b != NULL);
E = 1;
C = new double * [N];
for(int i = 0; i < N; i++)
{
C[i] = new double[N + 1];
}
for(row = 0; row < N; row++)
{
for(col = 0; col <= N; col++)
{
if(col < N)
{
C[row][col] = A[row][col];
}
else if(col == N)
{
C[row][col] = b[row];
}
cout.width(10);
cout.precision(2);
cout.setf(ios::fixed);
cout << C[row][col];
}
cout << endl;
}
cout << "Start Gaussian Elimination" << endl;
gaussianElimination();
for(row = 0; row < N; ++row)
{
for(col = 0; col <= N; ++col)
{
cout.width(10);
cout.precision(2);
cout.setf(ios::fixed);
cout << C[row][col];
}
}
}
Any suggestions, tips, or ideas are appreciated! Thanks...