Gauss Jordan Code

This is the C++ source code for Gaussian Jordan or Gauss Jordan Method.
#include <iostream>
#include <iomanip>
#include <iostream>
#include <conio.h>
#include <math.h>
using namespace std;


class GaussJordan
{
    public:
        GaussJordan();
        void setMatA();
        void setMatB();
        void solve();
        void setRowCol();
        bool pivotZero(int row);
        void normalize(int row);
        void exchangeRow(int row);
        void makeBelowPivotZero(int row);
        void makeAbovePivotZero(int row);
        void display();

    protected:

    private:
        double a[5][5],x[5];
        int rowA,colA;
};
GaussJordan::GaussJordan()
{
    //ctor
}
void GaussJordan::setRowCol()
{
    cout << "Enter row of Matrix A: ";
    cin >> rowA;
    cout << "Enter column of Matrix A: ";
    cin >> colA;
}
void GaussJordan::setMatA()
{
    cout << "For Matrix A : " << endl;
    for(int i =  0; i < rowA; i++ )
    {
        for(int j = 0; j < colA; j++)
        {

            cout << "Enter A[" << i << "]" << "[" << j << "] : " ;
            cin >> a[i][j];
        }
    }
}
void GaussJordan::setMatB()
{
    cout << "For Matrix B: " << endl;
    for(int i =  0; i < rowA; i++ )
    {

            cout << "Enter B[" << i << "]" << "[" << 0 << "] : " ;
            cin >> a[i][colA];
    }
}

void GaussJordan::solve()
{
    for(int i = 0; i < rowA; i++)
    {
        if(!pivotZero(i))
        {
            normalize(i);
        }
        else
        {
            exchangeRow(i);
            normalize(i);
        }
        makeBelowPivotZero(i);
    }

    for(int i = rowA-1; i > 0; i--)
    {
        makeAbovePivotZero(i);
    }
}

bool GaussJordan::pivotZero(int row)
{
    if(a[row][row] == 0)
    {
        return true;
    }
    else
        return false;
}

void GaussJordan::exchangeRow(int row)
{
    //cout << "Exchanfged\n";
    int k = row+1;
    int great = row+1;
    while (k<rowA)
    {
        if(a[great][row]<a[k][row])
            great  = k;
        k++;
    }
    //cout << great << endl;

    for (int j = 0; j < colA+1; j++)
    {
        double temp;
        temp = a[row][j];
        a[row][j] = a[great][j];
        a[great][j] = temp;
    }
    display();
}
void GaussJordan::normalize(int row)
{
    //cout << "normalized \n";
    double normal = a[row][row];
    for(int j = 0; j < colA+1; j++)
    {
        a[row][j] /= normal;
    }
    display();
}
void GaussJordan::makeBelowPivotZero(int row)
{
    //cout << "made zero" << endl;
    for(int i = row+1; i < rowA; i++)
    {
        double zeroFactor = a[i][row];
      //  cout << "zero = " << zeroFactor<< endl;
        for(int j = 0; j < colA+1; j++)
        {
            a[i][j] -= zeroFactor*a[row][j];
        }
        display();
    }

}
void GaussJordan::makeAbovePivotZero(int row)
{
    //cout << "made zero" << endl;
    for(int i = row-1; i >= 0; i--)
    {
        double zeroFactor = a[i][row];
       // cout << "zero = " << zeroFactor<< endl;
        for(int j = row; j < colA+1; j++)
        {
            a[i][j] -= zeroFactor*a[row][j];
        }
        display();
    }

}

void GaussJordan::display()
{
    for(int i = 0; i < rowA; i++)
    {
        for (int j = 0; j < colA+1; j++)
        {
            cout << a[i][j] << " ";
        }
        cout << endl;
    }

 cout << endl << endl;
}

int main()
{
    GaussJordan g1;
    cout << setprecision(4);
    g1.setRowCol();
    g1.setMatA();
    g1.setMatB();
    g1.solve();
    getch();
    return 0;
}

No comments:

Post a Comment