upsidedown

gauss seidel

Sep 23rd, 2011
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.08 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<math.h>
  3. #define e 0.001
  4. void main()
  5. {
  6.     double a[50][50],b[50],x[50];
  7.     double y,d,s=0,sum=0;
  8.     int n,j,i,f;
  9.     printf("enter the number of equation\n");
  10.     scanf("%d",&n);
  11.     do
  12.     {
  13.         f=1;
  14.         for(i=0;i<n;i++)
  15.         {
  16.             printf("enter the coefficients of equation:%d\n",i+1);
  17.             for(j=0;j<n;j++)
  18.                 scanf("%lf",&a[i][j]);
  19.             printf("enter the constant of the equation\n");
  20.             scanf("%lf",&b[i]);
  21.         //  for(j=0;j<(n-1);j++)
  22.         //      s=s+a[i][j];
  23.         //  s=s-a[i][i];
  24.         //  if(fabs(a[i][i])<fabs(s))
  25.         //  {
  26.         //      printf("the equation doesnt converge re-enter equations\n");
  27.         //      f=0;
  28.         //      break;
  29.         //  }
  30.         }
  31.     }while(f==0);
  32.     printf("enter the initial values ofx\n");
  33.     for(i=0;i<n;i++)
  34.         scanf("%lf",&x[i]);
  35.     do
  36.     {
  37.         f=1;
  38.         for(i=0;i<n;i++)
  39.         {
  40.             sum=0;
  41.             for(j=0;j<n;j++)
  42.             {
  43.                 if(i!=j)
  44.                     sum=sum+(a[i][j]*x[j]);
  45.             }
  46.             y=(b[i]-sum)/a[i][i];
  47.             printf("x%d=%.2lf\n",i+1,y);
  48.             d=fabs(y-x[i]);
  49.             x[i]=y;
  50.             if(d>=e)
  51.                 f=0;
  52.         }
  53.     }while(f==0);
  54.     printf("the values of x are:\n");
  55.     for(i=0;i<n;i++)
  56.         printf("x%d: %.4lf\n",i+1,x[i]);
  57. }
Advertisement
Add Comment
Please, Sign In to add comment