fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. void pib(double x[][5], int num){
  5. double c[5]={0};
  6. for(int j=0;j<4;j++){
  7. for(int i=1+j;i<num;i++){
  8. if(fabs(x[i][j])<fabs(x[i][j])){
  9. for(int n=0;n<5;n++){
  10. c[n] = x[j][n];
  11. x[j][n] = x[i][n];
  12. x[i][n] = c[n];
  13. }
  14. }
  15. for(int a=1+j;a<num;a++){
  16. double det = x[a][j]/x[j][j];
  17. for(int b=j;b<5;b++){
  18. x[a][b] -= x[j][b]*det;
  19. }
  20. }
  21. }
  22. }
  23. }
  24.  
  25. void gyo(double x[][5], int num){
  26. for (int i = 0; i < num; i++) {
  27. for (int j = 0; j < 5; j++) {
  28. printf("%f ", x[i][j]);
  29. }
  30. printf("\n");
  31. }
  32. }
  33.  
  34. void sol(double x[][5], int num){
  35. double x4 = x[3][4]/x[3][3];
  36. double x3 = (x[2][4]-x[2][3]*x4)/x[2][2];
  37. double x2 = (x[1][4]-x[1][3]*x4-x[1][2]*x3)/x[1][1];
  38. double x1 = (x[0][4]-x[0][3]*x4-x[0][2]*x3-x[0][1]*x2)/x[0][0];
  39. printf("x1 = %f, x2 = %f, x3 = %f, x4 = %f",x1,x2,x3,x4);
  40. }
  41.  
  42. int main(void) {
  43. double x[][5] = {
  44. {3, 1.5, -6, 4.8, 1.2},
  45. {1, 1.5, -2, -2.4, 0.6},
  46. {0, -1.5, -2, -1, -2.4},
  47. {2, 4, -1.8, -0.6, 0}};
  48. pib(x, 4);
  49. gyo(x, 4);
  50. sol(x, 4);
  51. return 0;
  52. }
  53.  
Success #stdin #stdout 0s 5280KB
stdin
Standard input is empty
stdout
3.000000 1.500000 -6.000000 4.800000 1.200000 
0.000000 1.000000 0.000000 -4.000000 0.200000 
0.000000 0.000000 -2.000000 -7.000000 -2.100000 
0.000000 0.000000 0.000000 0.500000 -3.710000 
x1 = 81.052000, x2 = -29.480000, x3 = 27.020000, x4 = -7.420000