You are on page 1of 3

#include <iostream>

using namespace std;


//function prototype
double** matrixArray(int a, int b);
double* vectorVars(int a);
void matrixPrint(double** p, int num_of_equation, double* v);
void matrixFree(double** p, int n);
void vectorFree(double* p);
void vectorPrint(double* p, int n);

int main()
{
int num_of_equation, num_of_cols, num_of_rows;
double** Array_size;
double* Vector_vars;
cout<<endl;
cout<<"
GAUSSIAN ELIMINATION"<<endl;
cout<<endl;
cout<<"Enter the number of equations: ";
cin>>num_of_equation;

Array_size = matrixArray(num_of_equation, num_of_equation);


/*-----------------------------------------ROWS AND COLUMS--------------------*/

cout<<"Enter variabes for rows and columns:"<<endl<<endl;


for(num_of_rows = 0; num_of_rows < num_of_equation; num_of_rows++)
for(num_of_cols = 0; num_of_cols < num_of_equation;
num_of_cols++)
cin>>Array_size[num_of_rows][num_of_cols];

/*---------------------------------------VECTOR VARIABLES---------------------*/
Vector_vars = vectorVars(num_of_equation);
cout<<"Enter right-hand vectors: "<<endl<<endl;
for(num_of_rows = 0; num_of_rows < num_of_equation; num_of_rows++)
cin>>Vector_vars[num_of_rows];

/*-------------------------PRINTS THE MATRIX----------------*/


cout << endl << endl;
cout << "AUGMENTED MATRIX:" << endl;
cout << endl;
cout << endl;
matrixPrint(Array_size, num_of_equation, Vector_vars);
/*------------------------AUGMENTED MATRIX TO UPPER TRIANGULAR FORM------------------------*/
for (int temp = 0; temp < num_of_equation-1; temp++)
{

for (num_of_rows =temp + 1; num_of_rows < num_of_equation;


num_of_rows++)
{
double x =
Array_size[num_of_rows][temp]/Array_size[temp][temp];
for (num_of_cols= temp+1; num_of_cols <
num_of_equation ; num_of_cols++)
Array_size[num_of_rows][num_of_cols] =
Array_size[num_of_rows][num_of_cols] -Array_size[temp][num_of_cols]*x;
Vector_vars[num_of_rows] =
Vector_vars[num_of_rows] - Vector_vars[temp]*x;
}
}
cout << endl << endl;
/*------------------------------PRINTS UPPER TRIANGULAR FORM------------------------*/
cout << "UPPER TRIANGULAR FORM: " << endl;
cout << endl;
cout << endl;
matrixPrint(Array_size, num_of_equation, Vector_vars);
/*---------------BACK SUBSTITUTION--------------------*/
Vector_vars[num_of_equation-1]= Vector_vars[num_of_equation1]/Array_size[num_of_equation-1][num_of_equation-1];
for ( int i = num_of_equation-2; i >= 0; i--)
{
double sum = Vector_vars[i];
for (int j = i+1; j < num_of_equation; j++)
sum = sum - Array_size[i][j]*Vector_vars[j];
Vector_vars[i] = sum/Array_size[i][i];
}
cout << endl << endl;
/*----------------------PRINTS SOLUTION--------------------*/
cout << "SOLUTION:" << endl;
vectorPrint(Vector_vars, num_of_equation);
matrixFree(Array_size, num_of_equation);
vectorFree(Vector_vars);
return 0;
}
/*--------------------------------FUNCTION THAT DELETES THE VALUE OF Array_size[] FROM
THE TRIANGULAR MATRIX TO REPLACE IT WITH THE Array_size[i] VALUE FROM
THE BACK-SUBSTITION------------------------------*/
void matrixFree(double** p, int n)
{
for ( int i = 0; i < n; i++)
delete[] p[i];
delete[] p;

p = 0;
}
/*--------------------------------FUNCTION THAT DELETES THE VALUE OF Vector_vars[] FROM
THE TRIANGULAR MATRIX TO REPLACE IT WITH THE Vector_vars[i] VALUE FROM
THE BACK-SUBSTITION------------------------------*/
void vectorFree(double* p)
{
delete[] p;
p = 0;
}
/*---------------------------------FUNCTION THAT PRINTS THE UPPER TRIANGULAR FORM-------------------------------*/
void vectorPrint(double* p, int n)
{
for ( int i = 0; i < n; i++)
cout <<"\t"<<"X"<<i<<"= "<<p[i];
cout << endl << endl;
}
/* ----------------function that tests if the variables inserted fits in the size of the
rows and
columns of the array which was declared by the inputted variable
num_of_equations ---------------- */
double** matrixArray(int a, int b)
{
double** p;
p = new double* [a];
for ( int i = 0; i < a; i++ )
p[i] = new double[b];
return p;
}
double* vectorVars(int a)
{
double* v;
v = new double[a];
return v;
}
/*-----------------------FUNCTION FOR PRINTING THE AUGMENTED MATRIX--------------------*/
void matrixPrint(double** p, int num_of_equation, double* v)
{
for ( int num_of_rows = 0; num_of_rows < num_of_equation; num_of_rows++)
{
for ( int num_of_cols = 0; num_of_cols < num_of_equation; num_of_cols++)
cout << "\t"<< p[num_of_rows][num_of_cols];
cout<<"\t\t"<<"|"<<"\t"<<v[num_of_rows];
cout << endl;
}
}

You might also like