# polly study cs

posts - 34, comments - 0, trackbacks - 0, articles - 1
C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

1#include <stdio.h>
2#include <math.h>
3#include <stdlib.h>
4/*
50.500000 1.100000 3.100000
60.000000 -10.040000 -24.500000
70.000000 0.000000 -12.284024
8-2.600000
91.000000
102.000000
11请按任意键继续. . .
12*/

13#define n 3
14double a[3][3]={{0.5,1.1,3.1},{5,0.96,6.5},{2,4.5,0.36}};
15double b[3]={6,0.96,0.02};
16double m[3][3];
17double x[3]={0,0,0};
18int k,p;
19
20int main(){
21    for(k=0;k<n-1;k++){
22            if(a[k][k]==0)  break;
23            else{
24                     for(int i=k+1;i<n;i++){
25                             m[i][k]=a[i][k]/a[k][k];
26                             for(int j=k;j<n;j++){
27                             a[i][j]=a[i][j]-m[i][k]*a[k][j];
28                             }

29                             b[i]=b[i]-b[k]*m[i][k];
30                     }

31            }

32    }

33    for(int i=0;i<n;i++){
34            for(int j=0;j<3;j++){
35            printf("%f ",a[i][j]);}

36            printf("\n");
37    }

38    //huidai--有错
39    x[n-1]=b[n-1];//Right..
40    for(int i=n-1;i>=0;i--){//where is the error
41          double sum=0;//ok.Vectory.
42          for(int j=i+1;j<n;j++){//Be careful while using for loop!!!
43                sum+=a[i][j]*x[j];
44          }

45          x[i]=(b[i]-sum)/a[i][i];
46    }

47
48    for(int i=0;i<n;i++){
49            printf("%3.2f\n",x[i]);
50    }

51
52    system("pause");
53}

54