Search This Blog

C-program tosolve a matrix in Gauss Elimination method

C-program to solve a matrix in Gauss Elimination method

/*Objective :To find out the unknowns (x,y,z) using Gauss elimination method*/

#include"stdio.h"
#include"conio.h"
#include"math.h"
void main()
{
int i,j;
float a[3][4],b[3][4],c[4][4];
float x,y,z,p,q,r;
m:
printf("\nEnter the coefficients: ");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%f",&a[i][j]);
printf("\nEnter the constants: ");
for(i=0;i<3;i++)
scanf("%f",&a[i][3]);

if(a[0][0]!=0.0)
{
p=a[1][0];
q=a[0][0];
r=a[2][0];
for(j=0;j<=3;j++)
{
b[0][j]=-(p/q)*a[0][j];
a[1][j]+=b[0][j];
c[0][j]=-(r/q)*a[0][j];
a[2][j]+=c[0][j];
}
p=a[2][1];
q=a[1][1];
for(j=0;j<=3;j++)
{
b[1][j]=-(p/q)*a[1][j];
a[2][j]+=b[1][j];
}
printf("\n\nThe matrix becomes\n");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
printf("%.4f\t",a[i][j]
}
printf("\n");
}
z=a[2][3]/a[2][2];
y=(a[1][3]-a[1][2]*z)/a[1][1];
x=(a[0][3]-a[0][2]*z-a[0][1]*y)/a[0][0];
printf("\nThe solution is");
printf("\nX=%f, Y=%f , Z=%f",x,y,z);
}
else
{
printf("\nThe first cofficient must not be zero,Enter again");
goto m;
}
}