Input : 2y + z = 4 x + y + 2z = 6 2x + y + z = 7
#include <stdio.h>
#include <math.h>
void solution( float a[3][4], int var );
int main()
{
int var, i, j, k, l, n;
var=3; //the number of variables: x1,x2,x3
float a[3][4]={
{1.0,1,2,6},
{0.0,2,1,4},
{2.0,1,1,7} };
printf("the orginal function :\n");
for (i=0;i<var;i++)
{
for (j=0;j<=var;j++)
printf("%0.2lf ",a[i][j]);
printf("\n");
}
printf("---------------------------\n");
solution( a, var );
return 0;
}
void solution( float a[3][4], 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("%0.2lf ",a[i][j]);
}
printf("\n");
}
printf("--------------------------\n");
}
printf( "\nSolutions:" );
for ( i = 0;i < var;i++ )
{
printf( "\nTHE VALUE OF x%d IS %0.2lf\n", i + 1, ( float ) a[ i ][ var ] / ( float ) a[ i ][ i ] );
}
}
輸出畫面
the orginal function :
1.00 1.00 2.00 6.00
0.00 2.00 1.00 4.00
2.00 1.00 1.00 7.00
---------------------------
1.00 1.00 2.00 6.00
0.00 2.00 1.00 4.00
0.00 -1.00 -3.00 -5.00
--------------------------
2.00 0.00 3.00 8.00
0.00 2.00 1.00 4.00
0.00 0.00 -5.00 -6.00
--------------------------
-10.00 -0.00 0.00 -22.00
-0.00 -10.00 0.00 -14.00
0.00 0.00 -5.00 -6.00
--------------------------
Solutions:
THE VALUE OF x1 IS 2.20
THE VALUE OF x2 IS 1.40
THE VALUE OF x3 IS 1.20
沒有留言:
張貼留言