2014-04-25 65 views
0

[解決方法謝謝]我在C++中開發了下面的代碼來解決利用Gauss-Seidel方法的線性方程,但我在填充陣列時在運行時似乎有問題我無法弄清楚。這裏是我的代碼...Gauss-Seidel方法來解非線性方程

#include<stdio.h> 
int main(void) 
{ 
    float a[10][10],b[10],x[10],y[10]; 
    int n=0,m=0,i=0,j=0; 
    printf("Enter size of 2d array(Square matrix) : "); 
    scanf("%d",&n); 
    for(i=0;i<n;i++) 
    { 
     for(j=0;j<n;j++) 
     { 
      printf("Enter values no. %d %d :",i,j); 
      scanf("%f",&a[i][j]); 
     } 
    } 
    printf("\nEnter Values to the right side of equation\n"); 
    for(i=0;i<n;i++) 
    { 
      printf("Enter values no. %d :",i,j); 
      scanf("%f",&b[i]); 
    } 
    printf("Enter initial values of x\n"); 
    for(i=0;i<n;i++) 
    { 
     printf("Enter values no. %d :",i); 
     scanf("%f",&x[i]); 
    } 
    printf("\nEnter the no. of iteration : "); 
    scanf("%d",&m); 
    while(m>0) 
    { 
     for(i=0;i<n;i++) 
     { 
      y[i]=(b[i]/a[i][i]); 
      for(j=0;j<n;j++) 
      { 
       if(j==i) 
        continue; 
       y[i]=y[i]-((a[i][j]/a[i][i])*x[j]); 
       x[i]=y[i]; 
      } 
      printf("x%d = %f ",i+1,y[i]); 
     } 
     printf("\n\n"); 
     m--; 
    } 
    return 0; 

感謝提前:)

+0

這不會編譯。我設法編譯運行失敗後。你可以發佈工作解決方案,沒有運行時錯誤? – 4pie0

+0

@ privatedatapublicchannel2我試圖找出先生:( – user3490561

回答

1

你不分配第二尺寸爲EquationHolder。既然是2D矩陣,你也必須分配第二維。你的雙for循環更改爲以下:

float ** EquationHolder=new float *[3]; 
for (int i=0; i<NumEquations; i++) 
{ 
    EquationHolder[i] = new float[3]; 
    cout<<"Please Enter The Information Of Equation ("<<i+1<<")...\n"; 
    for (int j=0; j<NumEquations; j++) 
    { 
     cout<<"X"<<j+1<<": "; 
     cin>>EquationHolder[i][j]; 
    } 
} 

不過,我會建議使用std::vector<std::vector<double>>而不是C原料陣列的安全得多。

+0

謝謝先生...非常感謝:) – user3490561

+0

總是樂意幫助先生... – 101010

1

的第一件事情:

所有陣列分配存儲:

int NumEquations=3; 
// Equation Holder... 
float ** EquationHolder= new float *[3]; 
for (int i=0; i<NumEquations; i++) 
{ 
    EquationHolder[ i] = new float[3]; 
    // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 
    cout<<"Please Enter The Information Of Equation ("<<i+1<<")...\n"; 
    for (int j=0; j<NumEquations; j++) 
    { 
     cout<<"X"<<j+1<<": "; 
     cin>>EquationHolder[i][j]; 
    } 
} 

此外還有在主過程的錯誤:

#include <iostream> 
using namespace std; 

void GaussSeidel(int Iterations, float **EquationHolder, 
            float *EquationResultHolder) { 
    int InitialGuess = 0; 
    float x1 = 0, x2 = 0, x3 = 0; 
    while (InitialGuess < Iterations) { 
     x1 = (1/EquationHolder[0][0])* (EquationResultHolder[0]- 
       ((EquationHolder[0][1]) * x2)-((EquationHolder[0][2]) * x3)); 
     x2 = (1/EquationHolder[1][1])*(EquationResultHolder[1]- 
       ((EquationHolder[1][0]) * x1)-((EquationHolder[1][2]) * x3)); 
     x3 = (1/EquationHolder[2][2])*((EquationResultHolder[2]- 
       ((EquationHolder[2][0]) * x1)-((EquationHolder[2][1]) * x2))); 
     InitialGuess += 1; 
     cout << "---------------------Iteration #" << InitialGuess 
            << "---------------------" << std::endl; 
     cout << "X1: " << x1 << "\t" << x2 << "\t" << x3 << std::endl; 
    } 
} 

用法:

int main() { 
    int NumEquations = 3; 
    // Equation Holder... 
    float ** EquationHolder = new float *[3]; 
    for (int i = 0; i < NumEquations; i++) { 
     EquationHolder[ i] = new float[3]; 
     cout << "Please Enter The Information Of Equation(" << i + 1 << ")...\n"; 
     for (int j = 0; j < NumEquations; j++) { 
      cout << "X" << j + 1 << ": "; 
      cin >> EquationHolder[i][j]; 
     } 
    } 
    //... as before 

    for (int i = 0; i < NumEquations; i++) { // deallocate storage 
     delete [] EquationHolder[ i]; 
    } 
    delete [] EquationHolder; 

    return 0; 
} 

輸出:

(...)

請輸入所需的迭代:9

---------------------迭代# 1個---------------------

X1:4 -2 1.42857

-------------- -------迭代#2 ---------------------

X1:4.14286 -2.83333 2.10204

(...)

---------------------迭代#9 -------------- -------

X1:3.81631 -3.03054 2.36438

+0

謝謝先生...非常感謝:) – user3490561

0

您還沒有初始化EquationHolder的行。補充一點:

float ** EquationHolder = new float *[3]; 
for(int i=0; i<NumEquations; i++)  // ADD 
    EquationHolder[i] = new float[3]; // ADD 

此外,我建議你使用,而不是浮動更加精確,並且較少受到數字錯誤)。

+0

謝謝先生...多讚賞:) – user3490561