0

Hey...I'm working on a Gaussian elimination problem...my pivot doesn't seem to work correctly...the logic must be wrong. Also, does anyone know why "INFINITY" comes up as some of my random numbers chosen?

Thanks in advance for any help! :)

```
Matrix::Matrix():N(0)
{
//Empty Matrix by default
}
Matrix::Matrix(int n)
{
N = n;
A = new double * [N];
for(int i = 0; i < N; i++)
{
A[i] = new double [N];
}
s = new double [N];
b = new double [N];
for( int i = 0 ; i < N; ++i){
b[i] = 0.00;
}
}
void Matrix::generate_A()
{
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
A[i][j] = random();
cout.width(15);
cout.precision(2);
cout.setf(ios::fixed);
cout << A[i][j];
}
cout << endl;
}
}
void Matrix::generate_s()
{
for(int i = 0; i < N; i++)
{
s[i] = random();
cout.precision(2);
cout.setf(ios::fixed);
cout << s[i] << endl;
}
}
void Matrix::solve_b(const Matrix& aMatrix, const Matrix& sMatrix )
{
A = aMatrix.get_A();
s = sMatrix.get_s();
assert( A != NULL);
assert( s != NULL);
for(int i = 0; i < N; ++i)
{
for(int j = 0; j < N; ++j)
{
b[i] = b[i] + (A[i][j] * s[i]);
}
cout.precision(2);
cout.setf(ios::fixed);
cout << b[i] << endl;
}
}
void Matrix::pivot()
{
int start, i, k;
for(int j = 0; j < N; ++j)
{
start = i = j;
k = i + 1;
while(k < N)
{
if (C[i][j] >= C[k][j])
++k;
else
{
i = k;
++k;
}
}
int p = i;
if(C[p][j] == 0)
{
E = 0;
return;
}
if(p > start)
{
double temp;
for(int m = 0; m < N; ++m)
{
temp = C[start][m];
C[start][m] = C[p][m];
C[p][m] = temp;
cout.precision(2);
cout << C[start][m] << " switched with " << C[p][m] << endl;
}
}
}
return;
}
void Matrix::solve_x(const Matrix& aMatrix, const Matrix& bMatrix )
{
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(int row = 0; row < N; row++)
{
for(int 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 << "Does pivot work? " << endl;
pivot();
cout << "Looks like it " << endl;
}
int Matrix::get_N()
{
return(N);
}
double ** Matrix::get_A() const
{
return (A);
}
double * Matrix::get_s() const
{
return (s);
}
double * Matrix::get_b() const
{
return (b);
}
double ** Matrix::get_C()
{
return (C);
}
double Matrix::random()
{
double dividend;
double divisor;
double rand;
do
{
dividend = ( std::rand() % 1000);
divisor = ( std::rand() % 100);
rand = (static_cast <double>(dividend)) / (static_cast<double>(divisor));
}
while( dividend < divisor );
return (rand);
}
int setN()
{
int n;
do
{
cout << "\nEnter a positive integer n, the size of the (n x n) matrix : ";
cin >> n;
}
while(n < 0);
return(n);
}
int main()
{
srand(time(NULL));
int n = setN();
//Create matrix A
Matrix A(n);
cout << "\nMatrix A:\n";
A.generate_A();
//Create sol'n matrix s
Matrix s(n);
cout << "\nMatrix s:\n";
s.generate_s();
Matrix b(n);
cout << "\nMatrix b:\n";
b.solve_b(A, s);
Matrix x(n);
cout << "\nMatrix C:\n";
x.solve_x(A, b);
system("pause");
return (0);
}
```