Gauss Jordan Method In C

The Gauss-Jordan technique is also referred to as the Gauss-Jordan elimination algorithm. It presents a revised approach to the Gauss Elimination Method employed for resolving a set of linear equations.

It can be likened to and simpler than the Gauss Elimination Method as the latter involves carrying out two separate procedures, namely:

  • Creating an upper triangular matrix
  • Performing back substitution

However, when applying the Gauss-Jordan Elimination Method, the goal is to transform the given matrix into a diagonal matrix in reduced row echelon form.

An illustration of the Gauss-Jordan technique for solving a set of linear equations:

Input:

3x - 2y + z = 7

x + y + z = 2

2x - y - 3z = 1

Output:

Final Augmented Matrix is:

1 0 0 1

0 1 0 2

0 0 1 3

Result is: x = 1, y = 2, z = 3

Explanation:

In this instance, we perform row manipulations to convert the augmented matrix into row-echelon form and subsequently reduce it to reduced row-echelon form in order to solve the system of equations through the Gauss-Jordan elimination method. The ultimate configuration of the matrix allows for a direct extraction of the solutions for variables x, y, and z.

The row operations listed below were employed to transform the augmented matrix into its ultimate form:

Row2 = Row2 - 3 * Row1

Row3 = Row3 - 2 * Row1

Row1 = Row1 + Row2

Row3 = Row3 + Row2

Row1 = Row1 - 3 * Row3

Row2 = Row2 - 2 * Row3

The operations performed on these rows lead to the ultimate augmented matrix presented in the output. The values of variable x, y, and z, represented by 1, 2, and 3 respectively, are depicted in the final column of the matrix.

Here is the C program's source code for resolving a group of linear equations. The system compiles the C program effectively. Additionally, provided below is the program's result.

Code:

Example

#include<stdio.h>

void solution( int a[][20], int var );
int main()
{

    int a[ 20 ][ 20 ], var, i, j, k, l, n;
printf( "\nEnter the number of variables:\n" );
scanf( "%d", &var );

    for ( i = 0;i <var;i++ )
    {
printf( "\nEnter the equation%d:\n", i + 1 );

        for ( j = 0;j <var;j++ )
        {
printf( "Enter the coefficient of  x%d:\n", j + 1 );
scanf( "%d", &a[ i ][ j ] );
        }

printf( "\nEnter the constant:\n" );
scanf( "%d", &a[ i ][ var] );
    }

solution( a, var );
    return 0;
}
void solution( int a[ 20 ][ 20 ], int var )
{
    int k, i, l, j;

    for ( k = 0;k <var;k++ )
    {
        for ( i = 0;i <= var;i++ )
        {
            l = a[i ][ k ];

            for ( j = 0;j <= var;j++ )
            {
                if ( i != k )
                a[i][j] = (a[k][k]*a[i][j])-(l*a[k][j]);
            }
        }
    }

printf( "\nSolutions:" );

    for ( i = 0;i <var;i++ )
    {
printf( "\nTHE VALUE OF x%d IS %f\n", i + 1, ( float ) a[ i ][ var ] / ( float ) a[ i ][ i ] );
    }

}

Output:

Output

Testcase Input:
Enter the number of variables: 4

Enter the equation1:
Enter the coefficient of x1: 2
Enter the coefficient of x2: 1
Enter the coefficient of x3: -3
Enter the coefficient of x4: 4
Enter the constant: 9

Enter the equation2:
Enter the coefficient of x1: 1
Enter the coefficient of x2: -2
Enter the coefficient of x3: 4
Enter the coefficient of x4: 3
Enter the constant: 7

Enter the equation3:
Enter the coefficient of x1: 3
Enter the coefficient of x2: 1
Enter the coefficient of x3: 5
Enter the coefficient of x4: -2
Enter the constant: 4

Enter the equation4:
Enter the coefficient of x1: 2
Enter the coefficient of x2: -5
Enter the coefficient of x3: 2
Enter the coefficient of x4: 1
Enter the constant: -6

Solutions:
THE VALUE OF x1 IS 1.000000
THE VALUE OF x2 IS 2.000000
THE VALUE OF x3 IS 3.000000
THE VALUE OF x4 IS -1.000000

Explanation:

The provided input represents the subsequent set of linear equations:

2x1 + x2 - 3x3 + 4x4 = 9

x1 - 2x2 + 4x3 + 3x4 = 7

3x1 + x2 + 5x3 - 2x4 = 4

2x1 - 5x2 + 2x3 + x4 = -6

The software employs the Gauss-Jordan technique to resolve the equation system, yielding the values 1.000000, 2.000000, 3.000000, and -1.000000 for x1, x2, and x3, respectively. These numerical results represent the solution to the system as they satisfy all four equations. This constitutes a comprehensive overview of the Gauss-Jordan method.

Input Required

This code uses input(). Please provide values below: