Answer to Question #99256 in C++ for unique timsina

Question #99256
Gauss Elimination method
1
Expert's answer
2019-11-24T15:41:03-0500
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS