Gaussian elimination Algorithm

Gaussian elimination algorithm is a fundamental technique in linear algebra that is utilized to solve systems of linear equations, find the rank of a matrix, and calculate the inverse of an invertible square matrix. It is named after the famous mathematician Carl Friedrich Gauss, who made significant contributions to the field of mathematics. The algorithm systematically transforms a given matrix into its row echelon form or reduced row echelon form through a series of elementary row operations. These row operations include swapping rows, multiplying a row by a nonzero constant, and adding or subtracting a multiple of one row to another row. The primary objective of Gaussian elimination is to simplify the given matrix in such a way that the resulting triangular matrix can be easily used to determine the unknown variable values. The Gaussian elimination algorithm consists of two main steps: forward elimination and back substitution. In the forward elimination phase, the algorithm iteratively eliminates variables from the equations by performing row operations until it reaches an upper triangular matrix. At this stage, the system of linear equations has been transformed into an equivalent but simpler form, with the same solution set as the original system. In the back substitution phase, the algorithm starts with the last equation and works its way up to the first equation, substituting the already known variable values to find the remaining unknowns. The process of back substitution concludes when all the unknown variables have been determined. Gaussian elimination is a versatile and efficient method for solving linear systems, and it serves as the foundation for many advanced numerical linear algebra techniques, such as LU decomposition and the QR factorization.
#include <iostream>
using namespace std;

int main()
{
    int mat_size, i, j, step;

    cout << "Matrix size: ";
    cin >> mat_size;

    double mat[mat_size + 1][mat_size + 1], x[mat_size][mat_size + 1];

    cout << endl
         << "Enter value of the matrix: " << endl;
    for (i = 0; i < mat_size; i++)
    {
        for (j = 0; j <= mat_size; j++)
        {
            cin >> mat[i][j]; //Enter (mat_size*mat_size) value of the matrix.
        }
    }

    for (step = 0; step < mat_size - 1; step++)
    {
        for (i = step; i < mat_size - 1; i++)
        {
            double a = (mat[i + 1][step] / mat[step][step]);

            for (j = step; j <= mat_size; j++)
                mat[i + 1][j] = mat[i + 1][j] - (a * mat[step][j]);
        }
    }

    cout << endl
         << "Matrix using Gaussian Elimination method: " << endl;
    for (i = 0; i < mat_size; i++)
    {
        for (j = 0; j <= mat_size; j++)
        {
            x[i][j] = mat[i][j];
            cout << mat[i][j] << " ";
        }
        cout << endl;
    }
    cout << endl
         << "Value of the Gaussian Elimination method: " << endl;
    for (i = mat_size - 1; i >= 0; i--)
    {
        double sum = 0;
        for (j = mat_size - 1; j > i; j--)
        {
            x[i][j] = x[j][j] * x[i][j];
            sum = x[i][j] + sum;
        }
        if (x[i][i] == 0)
            x[i][i] = 0;
        else
            x[i][i] = (x[i][mat_size] - sum) / (x[i][i]);

        cout << "x" << i << "= " << x[i][i] << endl;
    }
    return 0;
}

LANGUAGE:

DARK MODE: