#include <iostream>
#include <iomanip>
#include <iostream>
#include <conio.h>
#include <math.h>
using namespace std;
class GaussPivot
{
public:
void setMatA();
void setMatB();
void solve();
void setRowCol();
bool greatPivot(int row);
void normalize(int row);
void exchangeRow(int row);
void makeBelowPivotZero(int row);
void substitute();
void display();
protected:
private:
double a[5][5],x[5];
int rowA,colA;
};
void GaussPivot::setRowCol()
{
cout << "Enter row of Matrix A: ";
cin >> rowA;
cout << "Enter column of Matrix A: ";
cin >> colA;
}
void GaussPivot::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 GaussPivot::setMatB()
{
cout << "For Matrix B: " << endl;
for(int i = 0; i < rowA; i++ )
{
cout << "Enter B[" << i << "]" << "[" << 0 << "] : " ;
cin >> a[i][colA];
}
}
void GaussPivot::solve()
{
for(int i = 0; i < rowA; i++)
{
if(greatPivot(i))
{
normalize(i);
}
else
{
exchangeRow(i);
normalize(i);
}
makeBelowPivotZero(i);
}
substitute();
}
void GaussPivot::substitute()
{
x[rowA-1] = a[rowA-1][colA];
for(int i = rowA-2; i>=0 ;i--)
{
double s = 0;
for(int j = i+1; j < colA; j++)
{
s+= a[i][j] * x[j];
}
x[i] = a[i][colA] - s;
}
cout << endl << "The Required values are:\n";
for(int i = 0; i < rowA;i++)
cout << "X["<<i<<"]: "<< x[i] << endl;
}
bool GaussPivot::greatPivot(int row)
{
// cout << "pivot Check" << endl;
for(int i = row+1; i < rowA; i++)
{
if(fabs(a[row][row]) < fabs(a[i][row]))
{
// cout << a[i][row]<<"great" << endl;
display();
return false;
}
}
return true;
}
void GaussPivot::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 GaussPivot::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 GaussPivot::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 GaussPivot::display()
{
cout << setprecision(4) << endl;
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()
{
GaussPivot g1;
g1.setRowCol();
g1.setMatA();
g1.setMatB();
g1.solve();
getch();
return 0;
}
C and C++ programs, games, softwares. Easy programming guide. Learn to code and enjoy coding in CoderNepal
Home » C++ Program code for Gauss Elimination with Pivoting
C++ Program code for Gauss Elimination with Pivoting
Solution for Gauss Elimination with pivoting using C++ Program Code
Subscribe to:
Comments (Atom)
No comments:
Post a Comment