fork(1) download
  1. #include<stdio.h>
  2. #include<math.h>
  3. void
  4. show(float a[100][100],int n,int m)
  5. {
  6. int i,j;
  7. for(i=0;i<n;i++)
  8. {
  9. for(j=0;j<m;j++)
  10. {
  11. printf("%f ",a[i][j]);
  12. }
  13. printf("\n");
  14. }
  15.  
  16. }
  17. int
  18. main()
  19. {
  20. float a[100][100];
  21. float t,a1[100],sum=0;
  22. int p,r,c,n,m,k;
  23. int bool=1;
  24. float x;
  25. int i,j;
  26. scanf("%d%d",&n,&m);
  27. for(i=0;i<n;i++)
  28. {
  29. for(j=0;j<m;j++)
  30. {
  31. scanf("%f",&a[i][j]);
  32. }
  33. }
  34. p=0;
  35. for(i=0;i<n;i++)
  36. {
  37. bool=1;
  38. for(j=0;j<m;j++)
  39. {
  40. if(a[i][j]!=0)
  41. {
  42. bool=0;
  43. break;
  44. }
  45. }
  46.  
  47.  
  48. if(bool==1)
  49. {
  50. for(k=0;k<m;k++)
  51. {
  52.  
  53. t=a[n][k];
  54. a[n][k]=a[i][k];
  55. a[i][k]=t;
  56. }
  57.  
  58. n--;
  59. }
  60.  
  61. }
  62.  
  63. while(p<n&&p<m)
  64. {
  65. jump:
  66. r=1;
  67. while(a[p][p]==0)
  68. {
  69. if(p+r>n-1)
  70. {
  71. p=p+1;
  72. goto jump;
  73. }
  74. for(c=0;c<m;c++)
  75. {
  76. t=a[p+r][c];
  77. a[p+r][c]=a[r][c];
  78. a[r][c]=t;
  79. }
  80. r=r+1;
  81. }
  82. for(r=1;r<n-p;r++)
  83. {
  84.  
  85. x=(a[p+r][p])/a[p][p];
  86. if(a[p+r][p]!=0)
  87. {
  88. for(c=0;c<m;c++)
  89. {
  90. a[p+r][c]=(a[p][c]*x)-a[p+r][c];
  91. }
  92. }
  93. }
  94. p++;
  95. printf("\n");
  96. }
  97. show(a,n,m);
  98.  
  99. for(i=0;i<100;i++)
  100. {
  101. a1[i]=(float)1;
  102. }
  103. for(i=n-1;i>=0;i--)
  104. {
  105. sum=0;
  106. for(j=m-2;j>i;j--)
  107. {
  108. sum+=a1[j]*a[i][j];
  109. }
  110.  
  111. if(i<n-1)
  112. {
  113. a[i][m-1]=a[i][m-1]-sum;
  114. }
  115. if(i==n-1)
  116. {
  117. sum+=a1[j]*a[i][j];
  118. }
  119. a1[i]=a[i][m-1]/a[i][j];
  120. }
  121. for(i=0;i<=n-1;i++)
  122. printf("\n%dst variable is %f \n",i+1,a1[i]);
  123. return 0;
  124. }
  125.  
stdin
3
4
3 4 1 6
2 -1 2 -5
1 3 -1 9
compilation info
prog.c: In function ‘main’:
prog.c:26: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
prog.c:31: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
stdout


3.000000 4.000000 1.000000 6.000000 
0.000000 3.666667 -1.333333 9.000000 
-0.000000 0.000000 -0.727273 2.909091 

1st variable is 2.000000 

2st variable is 1.000000 

3st variable is -4.000000