fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. /* Guss-Jordan */
  5. #define N 3
  6. int main() {
  7. int i, j, k, x, y;
  8. double r, max;
  9. double a[N][N + 1] = {
  10. { 2.0, 3.0, 1.0, 4.0 },
  11. { 4.0, 1.0, -3.0, -2.0 },
  12. {-1.0, 2.0, 2.0, 2.0 } };
  13.  
  14. /* forward */
  15. for (i = 0; i < N; i++) {
  16. /* pivotting */
  17. max = fabs(a[i][i]);
  18. k = i;
  19. for (j = i + 1; j < N; j++)
  20. if (max < fabs(a[j][i])) {
  21. max = fabs(a[j][i]);
  22. k = j;
  23. }
  24. if (k != i)
  25. for (j = 0; j <= N; j++) {
  26. r = a[k][j];
  27. a[k][j] = a[i][j];
  28. a[i][j] = r;
  29. }
  30. /* pivotting finished */
  31.  
  32. r = a[i][i];
  33. for (j = i ; j <= N; j++)
  34. a[i][j] /= r;
  35.  
  36. for (j = i + 1; j < N; j++) {
  37. r = a[j][i];
  38. for (k = 0; k <= N; k++) {
  39. a[j][k] /= r;
  40. a[j][k] -= a[i][k];
  41. }
  42. }
  43. }
  44.  
  45. /* backward */
  46. for (i = N - 1; i >= 0; i--) {
  47. for (j = i - 1; j >= 0; j--) {
  48. a[j][N] -= a[i][N] * a[j][i];
  49. a[j][i] = 0.0;
  50. }
  51. }
  52. for (y = 0; y < N; y++) {
  53. for (x = 0; x <= N; x++)
  54. printf("%+.2f ", a[y][x]);
  55. putchar('\n');
  56. }
  57. putchar('\n');
  58. return 0;
  59. }
  60. /* end */
  61.  
Success #stdin #stdout 0.02s 1720KB
stdin
Standard input is empty
stdout
+1.00 +0.00 +0.00 +2.00 
+0.00 +1.00 +0.00 -1.00 
+0.00 +0.00 +1.00 +3.00