fork download
  1. #include<stdio.h>
  2. #include<math.h>
  3. #define N 3
  4. void mprint (float a[][N],float b[N]);
  5.  
  6. int main ()
  7. {
  8.  
  9. int i,j,k=0;
  10. float a[N][3]={{10, 1, 1},
  11. {2, 10, 1},
  12. {1, 1, 5}},
  13. b[N]={12, 13, 7}, x[N],norm=1,c, delta, eps=0.01;
  14. printf ("nachal'naya:\n");
  15. mprint (a,b);
  16.  
  17. for(i=0;i<N;i++)
  18. x[i]=b[i];
  19.  
  20. while(norm>eps)
  21. {
  22. k=k+1;
  23. printf("%d\n",k);
  24. //переменная norm содержит в себе максимальную разницу между иксами
  25. norm=0;
  26. for(i=0;i<N;i++)
  27. {
  28. c=b[i];
  29. for(j=0;j<N;j++)
  30. if(i!=j)
  31. //переменная "с" представляет собой правую часть уавнения
  32. c=c-a[i][j]*x[j];
  33. c=c/a[i][i];
  34. delta=fabs(x[i]-c);
  35. if (delta>norm)
  36. norm=delta;
  37. x[i]=c;
  38. }
  39.  
  40. }
  41. for (j=0;j<N;j++)
  42. {if(j==1) printf("x=");
  43. printf ("\t %8.5f \n",x[j]);}
  44.  
  45. return 0;}
  46.  
  47. // функция печати расширенной матрицы
  48. void mprint (float a[][N],float b[N])
  49. {int i,j;
  50. for (i=0;i<N;i++)
  51. {for (j=0;j<N;j++)
  52. printf ("%8.2f",a[i][j]);
  53. printf ("\t%3.2f\n",b[i]);}
  54. printf ("\n");
  55. return;}
Success #stdin #stdout 0s 9416KB
stdin
Standard input is empty
stdout
nachal'naya:
   10.00    1.00    1.00	12.00
    2.00   10.00    1.00	13.00
    1.00    1.00    5.00	7.00

1
2
3
4
	  1.00018 
x=	  0.99998 
	  0.99997