Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<math.h>
- #define e 0.001
- void main()
- {
- double a[50][50],b[50],x[50];
- double y,d,s=0,sum=0;
- int n,j,i,f;
- printf("enter the number of equation\n");
- scanf("%d",&n);
- do
- {
- f=1;
- for(i=0;i<n;i++)
- {
- printf("enter the coefficients of equation:%d\n",i+1);
- for(j=0;j<n;j++)
- scanf("%lf",&a[i][j]);
- printf("enter the constant of the equation\n");
- scanf("%lf",&b[i]);
- // for(j=0;j<(n-1);j++)
- // s=s+a[i][j];
- // s=s-a[i][i];
- // if(fabs(a[i][i])<fabs(s))
- // {
- // printf("the equation doesnt converge re-enter equations\n");
- // f=0;
- // break;
- // }
- }
- }while(f==0);
- printf("enter the initial values ofx\n");
- for(i=0;i<n;i++)
- scanf("%lf",&x[i]);
- do
- {
- f=1;
- for(i=0;i<n;i++)
- {
- sum=0;
- for(j=0;j<n;j++)
- {
- if(i!=j)
- sum=sum+(a[i][j]*x[j]);
- }
- y=(b[i]-sum)/a[i][i];
- printf("x%d=%.2lf\n",i+1,y);
- d=fabs(y-x[i]);
- x[i]=y;
- if(d>=e)
- f=0;
- }
- }while(f==0);
- printf("the values of x are:\n");
- for(i=0;i<n;i++)
- printf("x%d: %.4lf\n",i+1,x[i]);
- }
Advertisement
Add Comment
Please, Sign In to add comment