//GAURAV MITTAL(2K9/EE/830)
//QUESTION 10. To find out the unknowns (x,y,z) using Gauss elimination method
#include"stdio.h"
#include"conio.h"
int 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: ");
printf("\n\n");
for(i=0;i<3;i++)
{
printf("\n");
printf("Enter a1 : ");
scanf("%f",&a[i][0]);
printf("Enter b1 : ");
scanf("%f",&a[i][1]);
printf("Enter c1 : ");
scanf("%f",&a[i][2]);
}
printf("\n\nEnter the constants: ");
printf("\n\n");
printf("Enter d1 : ");
scanf("%f",&a[0][3]);
printf("Enter d2 : ");
scanf("%f",&a[1][3]);
printf("Enter d3 : ");
scanf("%f",&a[0][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("\n\n\nThe solution is\n");
printf("\nX=%f,\tY=%f,\tZ=%f",x,y,z);
}
else
{
printf("\nThe first cofficient must not be zero,Enter again");
goto m;
}
getch();
return 0;
}
No comments:
Post a Comment