C Program to Implement Gauss Jordan Elimination Method
Posted by Superadmin on December 07 2015 02:13:27
#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
$ gcc bubblesort.c -o bubblesort
$ ./bubblesort
Enter the number of variables: 3
Enter the equation 1:
Enter the coefficient of x1: 1
Enter the coefficient of x2: 0
Enter the coefficient of x3: 0
Enter the constant: 2
Enter the equation 2:
Enter the coefficient of x1: 0
Enter the coefficient of x2: 1
Enter the coefficient of x3: 0
Enter the constant: 0
Enter the equation 3:
Enter the coefficient of x1: 0
Enter the coefficient of x2: 0
Enter the coefficient of x3: 1
Enter the constant: -1
Solutions:
THE VALUE OF x1 IS 2.000000
THE VALUE OF x2 IS 0.000000
THE VALUE OF x3 IS -1.000000