GAUSS JORDAN ELIMINATION
July 22, 2022
Solve The Given Linear Equations Systems By Using Gauss Jordan Elimination Method
Source code :-
#include <stdio.h>
int main()
{
int i, j, k, n;
float a[20][20], x[20], r;
printf("Enter the number of equations: ");
scanf("%d", &n);
// Input the matrix.
printf("\nEnter co-efficient matrix [A] row wise:\n");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
printf("\na[%d][%d]= ", i + 1, j + 1);
scanf("%f", &a[i][j]);
}
printf("\n");
}
printf("\n\nEnter column matrix [B] row wise:\n");
for (i = 0, j = n; i < n; i++)
{
printf("\nb[%d]= ", i + 1);
scanf("%f", &a[i][j]);
}
printf("\n");
// transform coefficient matrix into normalize form
for (k = 0; k < n; k++)
{
for (i = 0; i < n; i++)
{
if (i == k)
continue;
r = a[i][k] / a[k][k];
for (j = 0; j <= n; j++)
{
a[i][j] = a[i][j] - r * a[k][j];
}
}
// Steps display start. [optional]
printf("\nStep %d:-\n\n", k + 1);
for (i = 0; i < n; i++)
{
printf("|");
for (j = 0; j < n; j++)
{
printf("%5.2f", a[i][j]);
}
printf(" |\t| %5.2f |\n", a[i][j]);
}
printf("\n");
// Steps display stop.
}
for (i = 0; i < n; i++)
{
x[i] = a[i][n] / a[i][i];
a[i][i] = a[i][i] / a[i][i];
}
printf("\nStep %d:-\n\n", k + 1);
for (i = 0; i < n; i++)
{
printf("|");
for (j = 0; j < n; j++)
{
printf("%5.2f", a[i][j]);
}
printf(" |\t| %5.2f |\n", x[i]);
}
printf("\n");
printf("\nThe solution is:\n");
for (i = 0; i < n; i++)
{
printf("x[%d]=%5.2f\n", i + 1, x[i]);
}
return 1;
}
Input-Output :-
abhi@hp-15q-laptop:~$ gcc gauss_jordan.cabhi@hp-15q-laptop:~$ ./a.out
Enter the number of equations: 3
Enter co-efficient matrix [A] row wise:
a[1][1]= 2
a[1][2]= -1
a[1][3]= 3
a[2][1]= 1
a[2][2]= 1
a[2][3]= 1
a[3][1]= 1
a[3][2]= -1
a[3][3]= 1
Enter column matrix [B] row wise:
b[1]= 9
b[2]= 6
b[3]= 2
Step 1:-
| 2.00-1.00 3.00 | | 9.00 || 0.00 1.50-0.50 | | 1.50 || 0.00-0.50-0.50 | | -2.50 |
Step 2:-
| 2.00 0.00 2.67 | | 10.00 || 0.00 1.50-0.50 | | 1.50 || 0.00 0.00-0.67 | | -2.00 |
Step 3:-
| 2.00 0.00 0.00 | | 2.00 || 0.00 1.50 0.00 | | 3.00 || 0.00 0.00-0.67 | | -2.00 |
Step 4:-
| 1.00 0.00 0.00 | | 1.00 || 0.00 1.00 0.00 | | 2.00 || 0.00 0.00 1.00 | | 3.00 |
The solution is:x[1]= 1.00x[2]= 2.00x[3]= 3.00