Hello all.
After going through a deep search on these forums and google as a whole, I have not been able to find a correct piece of advice or input on how I should aproach Guassian Elimination in C++. Ive been assigned to make a program that can perform Guassian Elimination and Ive managed to establish a stable program but I ALWAYS recieve wrong outputs. If anyone could please look at this and give some input, I would honestly appreciate it. Ive spent many hours just debugging and playing with all the variables.

# include <iostream>
# include <cstdlib>
using namespace std; 


int main ()
{
int n;
int j=0;


//int n = 3; //number of equations
cout << "Enter the number of equations..."<< endl;
cin >> n;



double *b;
b = new double[n];
//b [0] = 0.0;
//b [1] = 3.0;
//b [2] = 2.0;
for (int i = 0; i<n; i++)
{ cout<< "Enter b["<<i<<"]:";
cin >> b[i];}


double *x;
x=new double [n];

double **A;
A = new double *[n];
for ( int i = 0; i<n; i++)
{
A [i] = new double [n];
}

//A [0] [0] = 1.0;
//A [0] [1] = 2.0;
//A [0] [2] = 3.0;
//A [1] [0] = 2.0;
//A [1] [1] = 2.0;
//A [1] [2] = 3.0;
//A [2] [0] = -1.0;
//A [2] [1] = -3.0;
//A [2] [2] = 0.0;
//{1.0, 2.0, 1.0 },
//{2.0, 2.0, 3.0},
//{-1.0, -3.0, 0.0} ;

    for (int i = 0; i<n; i++)

        for (int j = 0; j<n; j++)
        {
        cout << "Enter A["<< i <<"]["<< j <<"]:";
        cin >> A [i][j];
        }



for (int k = 0; k <= n-2; k++)
{   
    for (int i = k+1; i <= n-1; i++)
               {
                   double m = (-A [i] [k]) / (A [k] [k]);
                   A[i][k]=0;
                       for ( int j = k+1; j <= n; j++)
                       {A[i][j] += m* A[k][j];
                       }
               }

}
x [n-1] = (A [n-1][n]) /( A [n-1][n-1]);
for (int i =n-2; i >= 0; i--)
{


       for (int j=n-1; j >=i+1; j--)
       {A[i][n] -=x[j]*A[i][j];}

       x[i]=A[i][n]/A[i][i];
}



cout<< x[0]<<endl;
cout<< x[1]<<endl;
cout<< x[2]<<endl<<endl;

// The x's should be [1, -1, 1]
cout << A [0][0] <<"  "<<      A[0][1]    << "  "<<    A[0][2]     <<"  "<<    x[0] << "   =  "  <<   b[0] << endl;

cout << A [1][0] <<"  "<<      A[1][1]    << "  "<<    A[1][2]     <<"  "<<    x[1] << "   =  "  <<   b[1] << endl;

cout << A [2][0] <<"  "<<      A[2][1]    << "  "<<    A[2][2]     <<"  "<<    x[2] << "   =  "  <<   b[2] << endl;
}

Recommended Answers

All 4 Replies

try goggling "Gaussian elimination".

Trust me, Ive done that. Ive gone as far to testing other peoples code and comparing it with mine. Oddly though, I still dont recieve the correct results.

Repost using code tags and someone will probably take a look otherwise they'll just ignore it as it is too hard to read.
my previous post related to the spelling

# include <iostream>
# include <cstdlib>
using namespace std; 


int main ()
{
int n;
int j=0;


//int n = 3; //number of equations
cout << "Enter the number of equations..."<< endl;
cin >> n;



double *b;
b = new double[n];
//b [0] = 0.0;
//b [1] = 3.0;
//b [2] = 2.0;
for (int i = 0; i<n; i++)
{ cout<< "Enter b["<<i<<"]:";
cin >> b[i];}


double *x;
x=new double [n];

double **A;
A = new double *[n];
for ( int i = 0; i<n; i++)
{
A [i] = new double [n];
}

//A [0] [0] = 1.0;
//A [0] [1] = 2.0;
//A [0] [2] = 3.0;
//A [1] [0] = 2.0;
//A [1] [1] = 2.0;
//A [1] [2] = 3.0;
//A [2] [0] = -1.0;
//A [2] [1] = -3.0;
//A [2] [2] = 0.0;
//{1.0, 2.0, 1.0 },
//{2.0, 2.0, 3.0},
//{-1.0, -3.0, 0.0} ;

for (int i = 0; i<n; i++)

for (int j = 0; j<n; j++)
{
cout << "Enter A["<< i <<"]["<< j <<"]:";
cin >> A [i][j];
}



for (int k = 0; k <= n-2; k++)
{ 
for (int i = k+1; i <= n-1; i++)
{
double m = (-A [i] [k]) / (A [k] [k]);
A[i][k]=0;
for ( int j = k+1; j <= n; j++)
{A[i][j] += m* A[k][j];
}
}

}
x [n-1] = (A [n-1][n]) /( A [n-1][n-1]);
for (int i =n-2; i >= 0; i--)
{


for (int j=n-1; j >=i+1; j--)
{A[i][n] -=x[j]*A[i][j];}

x[i]=A[i][n]/A[i][i];
}



cout<< x[0]<<endl;
cout<< x[1]<<endl;
cout<< x[2]<<endl<<endl;

// The x's should be [1, -1, 1]
cout << A [0][0] <<" "<< A[0][1] << " "<< A[0][2] <<" "<< x[0] << " = " << b[0] << endl;

cout << A [1][0] <<" "<< A[1][1] << " "<< A[1][2] <<" "<< x[1] << " = " << b[1] << endl;

cout << A [2][0] <<" "<< A[2][1] << " "<< A[2][2] <<" "<< x[2] << " = " << b[2] << endl;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.