Monday, August 22, 2011

Program to calculate the unknowns of simultaneous linear equations



#include<stdio.h>
#include<math.h>
int main()
{
int i,j,k,n;
float c,a[100][101],x[100];
printf("Enter the no. of variables:");

scanf("%d",&n);
//input
printf("Enter coefficients and constants in the form of\n");
printf("%d by %d matrix:\n",n,n+1);
for(i=0;i<n;i++)
for(j=0;j<n+1;j++)
{
    scanf("%f",&a[i][j]);
}
//eliminating aij
for(j=0;j<n;j++)
for(i=0;i<n;i++)
if(i!=j)
{
 
if(fabs(a[j][j])<0.1)
{
printf("The solution does not converge by this method.");
return 1;
}
c=a[i][j]/a[j][j];
for(k=0;k<=n;k++)
a[i][k]-=c*a[j][k];
 
}
 
   //calculating solutions
for(i=0;i<n;i++)
x[i]=a[i][n]/a[i][i];
//displaying solutions
for(i=0;i<n;i++)
{
printf("x[%d]=%5.2f\n",i+1,x[i]);
}
return 0;

}

No comments:

Post a Comment