[问题] 高斯消去法

楼主: won54chan (王54丞)   2014-10-29 09:21:55
开发平台(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;
作者: dirkc (3781615)   2014-10-29 11:41:00
一个小建议,好好排版,你的睡眠时间或许可以因此多一些 :)
楼主: won54chan (王54丞)   2014-10-29 13:44:00
已解决 谢榭
作者: uorol (′‧ω‧‵)   2014-10-29 14:00:00
这排版太惨烈了...

Links booklink

Contact Us: admin [ a t ] ucptt.com