1 //++++++++++++++++++++++++++
2 // SystemEq.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 float dfCof[20][40];
16
17 ///system cls
18 system("cls");
19
20 ///Prompt input_eqs
21 printf("Enter the number of equations : ");
22
23 ///Keyin _eqs
24 scanf("%d", &iEqs);
25
26 ///Prompt input_cof
27 printf("\nEnter the coefficients of the equations :\n\n");
28
29 ///Xru 0 to colmax-1
30 for(i = 0 ; i < iEqs ; i++)
31 {
32 ///Xru 0 to rowmax-1
33 for(j = 0 ; j < iEqs ; j++)
34 {
35 ///Prompt input_Xij
36 printf("X%d%d = ", i + 1, j + 1);
37
38 ///Keyin COEFFICIENT-i.j
39 scanf("%f", &dfCof[i][j]);
40 dfCof[i][j+iEqs]=dfCof[i][j];
41
42 ///+
43 }//end for
44
45 ///Prompt input_Yij
46 printf("Y%d = ", i + 1);
47
48 ///Keyin COEFFICIENT rowmax
49 scanf("%f", &dfCof[i][iEqs*2]);
50
51 ///+
52 }//end for
53
54 ///matrix left_shift
55 int iShift;
56 for(iShift = iEqs ; iShift > 0 ; iShift--)
57 {
58 ///Copy Block dfCof2fCof
59 for(i = 0 ; i < iEqs ; i++) for(j = 0 ; j < iEqs ; j++) fCof[i][j] = dfCof[i][j+iShift];
60
61 ///Copy Y_col
62 for(i = 0 ; i < iEqs ; i++) fCof[i][iEqs] = dfCof[i][iEqs*2];
63
64 ///Reduce Cycle iRdsCyc=0,1 (0~iEQs-1)
65 int iRdsCyc;
66 for(iRdsCyc = 0 ; iRdsCyc < iEqs - 1 ; iRdsCyc++)
67 {
68 ///Renew row count iRenuRowCnt
69 int iRenuRowCnt;
70 for(iRenuRowCnt = iRdsCyc + 1 ; iRenuRowCnt < iEqs ; iRenuRowCnt++)
71 {
72 ///XpnOvrXnn
73 double XpnOvrXnn;
74 XpnOvrXnn = fCof[iRenuRowCnt][iRdsCyc] / fCof[iRdsCyc][iRdsCyc];
75
76 ///Renew row iRenuRow=4,3 =iRdsCyc~iEQs
77 int iRenuRow;
78 for(iRenuRow = iRdsCyc ; iRenuRow < iEqs + 1 ; iRenuRow++)
79 {
80 fCof[iRenuRowCnt][iRenuRow] = fCof[iRenuRowCnt][iRenuRow] - fCof[iRdsCyc][iRenuRow] *XpnOvrXnn ;
81 }
82
83 ///+
84 }//end for
85
86 ///+
87 }//end for Reduce Cycle
88
89 ///Xn=x[EQs-1] =Y/X
90 x[iShift-1] = fCof[iEqs-1][iEqs] / fCof[iEqs-1][iEqs-1];
91
92 ///+
93 }//end for
94
95 ///Prompt result
96 printf("\nThe result is :\n");
97
98 ///Display result
99 for(i = 0 ; i < iEqs ; i++) {printf("\nsolution of x%d = %.2f", i + 1, x[i]);}
100
101 ///system pause
102 printf("\n\n");
103 system("pause");
104
105 ///main_end
106 return 0;
107 } //main_end
108
This Blog is dedicated to Sir Johnson Lin
Solving a system of linear equations by Gaussian elimination(row reduction plus Matrix Rotation skill) 聯立方程式之高斯消去法併用矩陣旋轉技巧
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言