[問題] 高斯消去法

看板C_and_CPP作者 (王54丞)時間11年前 (2014/10/29 09:21), 11年前編輯推噓0(003)
留言3則, 3人參與, 最新討論串3/4 (看更多)
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) C++ 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 與正解不符 餵入的資料(Input): 預期的正確結果(Expected Output): x=[1 -1 2] 錯誤結果(Wrong Output): x=[2.5 2.5 1] 程式碼(Code):(請善用置底文網頁, 記得排版) #include<iostream> #include<cstdlib> #include<fstream> #include<stdio.h> #include<math.h> using namespace std; double A[3][4]; double x[3]; void gaussh(double matrix[3][4]); int main(void) { double A[3][4]={{-1,1,2,2},{3,-1,1,6},{-1,3,4,4}}; gaussh(A); for (int i=0; i<3; ++i) cout<<x[i]<<"\n"; system("pause"); return 0; } void gaussh(double matrix[3][4]) { for (int i=0; i<3; ++i) { if (matrix[i][i]==0) for (int j=i+1;j<4;j++) { if(matrix[j][i]!=0) for(int m=i;m<3;m++) swap(matrix[i][m],matrix[j][m]); } double t; for (int k=i; k<3; ++k) { t=matrix[k+1][k]/matrix[k][k] ; for (int j=i+1; j<4; ++j) { matrix[j][k] -= matrix[i][k] * t; } } } x[3]=matrix[3][4]/matrix[3][3]; for (int i=2; i>=0; --i) { double cc = 0; for (int k=i+1; k<3; ++k) cc += matrix[i][k]*x[k]; x[i] = (matrix[i][3]-cc) / matrix[i][i]; } return; } 補充說明(Supplement): 我照著書上及網路的程式碼打 還是錯 我從晚上看到天亮(真的...)還是不知道哪裡錯 初學程式語言,還請各位多包涵 請大大幫我解惑! -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.70.166.163 ※ 文章網址: http://www.ptt.cc/bbs/C_and_CPP/M.1414545719.A.B65.html

10/29 11:41, , 1F
一個小建議,好好排版,你的睡眠時間或許可以因此多一些 :)
10/29 11:41, 1F
※ 編輯: won54chan (61.70.166.163), 10/29/2014 12:38:44

10/29 13:44, , 2F
已解決 謝榭
10/29 13:44, 2F

10/29 14:00, , 3F
這排版太慘烈了...
10/29 14:00, 3F
文章代碼(AID): #1KK44tjb (C_and_CPP)
文章代碼(AID): #1KK44tjb (C_and_CPP)