This Blog is dedicated to Sir Johnson Lin
Learn flow chart from C language , master C with the chart!

Gaussian elimination(row reduction) to solve a system of linear equations with back-substitution method 高斯消去法解聯立方程式 使用反向代回法





D:\cppCoding\xi_code\SystemEq2\SystemEq2.c++
  1 //++++++++++++++++++++++++++
  2  //      SystemEq2.cpp
  3  //++++++++++++++++++++++++++
  4  
  5  ///#include header
  6  //#include<conio.h>
  7  #include<bits/stdc++.h>
  8  
  9  ///main_in
 10  int main(int argc,char *argv[])
 11  {
 12      ///declare
 13      int i, j, iEqs;
 14      float fCof[20][20], x[20];
 15      
 16      ///system cls
 17      system("cls");
 18      
 19      ///Prompt input_eqs
 20      printf("Enter the number of equations : ");
 21      
 22      ///Keyin _eqs
 23      scanf("%d", &iEqs);
 24      
 25      ///Prompt input_cof
 26      printf("\nEnter the coefficients of the equations :\n\n");
 27      
 28      ///Xru 0 to colmax-1
 29      for(i = 0 ; i < iEqs ; i++)
 30      {
 31          ///Xru 0 to rowmax-1
 32          for(j = 0 ; j < iEqs ; j++)
 33          {
 34              ///Prompt input_Xij
 35              printf("X%d%d = ", i + 1, j + 1);
 36              
 37              ///Keyin COEFFICIENT-i.j
 38              scanf("%f", &fCof[i][j]);
 39              
 40              ///+
 41          }//end for
 42          
 43          ///Prompt input_Yij
 44          printf("Y%d = ", i + 1);
 45          
 46          ///Keyin COEFFICIENT rowmax
 47          scanf("%f", &fCof[i][iEqs]);
 48          
 49          ///+
 50      }//end for
 51      
 52      ///Reduce Cycle iRdsCyc=0,1 (0~iEQs-1)
 53      int iRdsCyc;
 54      for(iRdsCyc = 0 ; iRdsCyc < iEqs - 1 ; iRdsCyc++)
 55      {
 56          ///Renew row count iRenuRowCnt
 57          int iRenuRowCnt;
 58          for(iRenuRowCnt = iRdsCyc + 1 ; iRenuRowCnt < iEqs ; iRenuRowCnt++)
 59          {
 60              ///XpnOvrXnn
 61              double XpnOvrXnn;
 62              XpnOvrXnn = fCof[iRenuRowCnt][iRdsCyc] / fCof[iRdsCyc][iRdsCyc];
 63              
 64              ///Renew row iRenuRow=4,3 =iRdsCyc~iEQs
 65              int iRenuRow;
 66              for(iRenuRow = iRdsCyc ; iRenuRow < iEqs + 1 ; iRenuRow++)
 67              {
 68                  fCof[iRenuRowCnt][iRenuRow] = fCof[iRenuRowCnt][iRenuRow] - fCof[iRdsCyc][iRenuRow] *XpnOvrXnn ;
 69              }
 70              
 71              ///+
 72          }//end for
 73          
 74          ///+
 75      }//end for Reduce Cycle
 76      
 77      ///Xn=x[EQs-1] =Y/X
 78      x[iEqs-1] = fCof[iEqs-1][iEqs] / fCof[iEqs-1][iEqs-1];
 79      
 80      ///[Esq-2]~0
 81      double iSops;
 82      for(i = iEqs - 2 ; i >= 0 ; i--)
 83      {
 84          ///Sum Of Product iSops= 0
 85          iSops = 0;
 86          
 87          ///Sop loop
 88          for(j = i + 1 ; j < iEqs ; j++)
 89          {
 90              ///x[i] =
 91              iSops  = iSops + (fCof[i][j] * x[j]);
 92              x[i] = (fCof[i][iEqs] - iSops) / fCof[i][i];
 93              
 94              ///+
 95          }//end for
 96          
 97          ///+
 98      }//end for
 99      
100      ///Prompt result
101      printf("\nThe result is :\n");
102      
103      ///Display result
104      for(i = 0 ; i < iEqs ; i++) {printf("\nsolution of x%d = %.2f", i + 1, x[i]);}
105      
106      ///system pause
107      printf("\n\n");
108      system("pause");
109      
110      ///main_end
111      return 0;
112  } //main_end
113  

@ 下載程式碼 Download source code

沒有留言:

張貼留言