#include <iostream>
using namespace std;
double** GaussElimination(double** matrix, int rows, int columns)
{
for (int i = 0; i < rows - 1; i++)
{
for (int k = i + 1; k < rows; k++)
{
if (matrix[k][i] != 0)
{
double t = matrix[k][i] / matrix[i][i];
for (int j = 0; j < columns; j++)
{
matrix[k][j] = matrix[k][j] - t * matrix[i][j];
}
}
}
}
return matrix;
}
void OutputMatrix(double** matrix, int rows, int columns) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
int main()
{
int rows = 3;
int columns = 3;
double** matrix = new double* [rows];
for (int i = 0; i < rows; i++) {
matrix[i] = new double[columns];
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
matrix[i][j] = i + j + 1;
}
}
cout << "Matrix before Gauss Elimination" << endl;
OutputMatrix(matrix, rows, columns);
cout << endl;
matrix = GaussElimination(matrix, rows, columns);
cout << "Matrix after Gauss Elimination" << endl;
OutputMatrix(matrix,rows,columns);
for (int i = 0; i < rows; i++) {
delete[] matrix[i];
}
delete matrix;
system("pause");
return 0;
}
Comments
Leave a comment