2011-05-11 41 views
0

其中一個示例問題是基於正割根的程序使用x,但不聲明它,但成功運行。同時,當我使用幾乎相同的設置時,我得到一個錯誤,說x是未聲明的。爲什麼它在示例程序中起作用,而不是我的?爲什麼x有效,但未申報? C++

using namespace std; 
#include<iostream> 
#include<cmath> 
#include<iomanip> 
// Declaration of functions used 
void secant(double, double, double, double, 
       double, double, double&, int&); 
double fx(double, double, double, double, double); 
const double tol=0.0001; // Tolerance for convergence 
const int max_iter=50;  // Maximum iterations allowed 
// main program 
int main() 
{ 
    int iteration;   // Number of iterations 
    double a, b, c, d;  // Constants in f(x) 
    double x0, x1;   // Starting values for x 
    double root;   // Root found by secant method 
    cout<<"Enter a, b, c, and d"<<endl<<"separated by a space "; 
    cin>>a>>b>>c>>d; 
    cout<<"\nEnter two initial values for x,\nseparated by a space: "; 
    cin>>x0>>x1; 
    secant(a, b, c, d, x0, x1, root, iteration);// Call "secant" 
// Output 
    cout<<"\nThe root is = "<<root<<endl; 
    cout<<"The number of iterations was = "<<iteration<<endl; 
    cout<<"The value of f(x) at the root = "<<fx(a,b,c,d,root)<<endl<<endl; 
system("pause"); 
return 0; 
} 
// Definition of function "secant" 
// Receives a, b, c, d and x0 values from main program 
// Returns root and the iterations required 
void secant(double a,double b, double c, double d, 
     double x0, double x1, double& root, int& iteration) 
{ 
    double xnminus1, xnplus1, xn; // Local variables 
    iteration=0;     // Initialize iterations 
    xnminus1=x0; 
    xn=x1; 
    do 
    { 
     ++iteration; 
     xnplus1 = xn - fx(a,b,c,d,xn)*(xn-xnminus1)/ 
            (fx(a,b,c,d,xn)-fx(a,b,c,d,xnminus1)); 
     cout<<"x"<<iteration+1<<" = "<<xnplus1<<endl; 
     xnminus1 = xn; 
     xn=xnplus1; 
    } 
    while ((fabs(fx(a,b,c,d,xnplus1)) >= tol)&& (iteration < max_iter)); 
    root=xnplus1; 
} 
// Defines "fx" -- calculates f(x) = ax^3+bx^2+cx+d 
double fx(double a, double b, double c, double d, double x) 
{ 
    return a*pow(x,3)+b*pow(x,2)+c*x+d; 
} 

這裏是我的程序,我用同樣的結構,但它是說x是未申報?

using namespace std; 
    #include<iostream> 
    #include<cmath> 
    #include<iomanip> 
    #include<fstream> 
    // Declaration of functions used 
    void secant(double, double, double, double, 
        double, double, double, double, double&, int&); 
    double fx(double, double, double, double, double, double, double); 
    const double tol=0.0001; // Tolerance for convergence 
    const int max_iter=50;  // Maximum iterations allowed 
    // main program 
    int main() 
    { 
     int iteration;   // Number of iterations 

     double kr, uc, q, b, radians; 

     double x0, x1;   // Starting values for x 
     double root;   // Root found by secant method 
    const double PI = 4.0*atan(1.0); 
    ifstream datain ("shuttle.txt"); 
    ofstream dataout ("results.txt"); 
    datain >> kr >> uc >> q >> b; 
    int velocity = 16000; 
    double angle =10; 
    x0= 1000; 
    x1 = 200; 
    for (int velocity = 16000; velocity <= 17500; velocity += 500) { 
     for (int angle = 10; angle <= 70; angle += 15) { 
    radians= angle * PI/180 ; 
        cout << velocity << endl; 
        cout << radians << endl; 
        cout << angle << endl; 
        secant (radians, velocity, kr, uc, q, b, x0, x1, root, iteration); 

    // Output 
     cout<<"\nThe root is = "<<root<<endl; 
     cout<<"The number of iterations was = "<<iteration<<endl; 
     cout<<"The value of f(x) at the root = "<<fx(radians, velocity, kr, uc, q, b, root)<<endl<<endl; 
    system("pause"); 
    return 0; 
    } 
    // Definition of function "secant" 
    // Receives a, b, c, d and x0 values from main program 
    // Returns root and the iterations required 
    void secant(double radians,double velocity, double kr, double uc, double q, double b, double x0, double x1, double& root, int& iteration); 
    { 
     double xnminus1, xnplus1, xn; // Local variables 
     iteration=0;     // Initialize iterations 
     xnminus1=x0; 
     xn=x1; 
     do 
     { 
      ++iteration; 
      xnplus1 = xn - fx(radians, velocity, kr, uc, q, b, xn)*(xn-xnminus1)/ 
             (fx(radians, velocity, kr, uc, q, b, xn)-fx(radians, velocity, kr, uc, q, b, xnminus1)); 
      cout<<"x"<<iteration+1<<" = "<<xnplus1<<endl; 
      xnminus1 = xn; 
      xn=xnplus1; 
     } 
     while ((fabs(fx(radians, velocity, kr, uc, q, b, xnplus1)) >= tol)&& (iteration < max_iter)); 
     root=xnplus1; 
    } 
    // Defines "fx" -- calculates f(x) = ax^3+bx^2+cx+d 
    double fx(double radians,double velocity, double kr, double uc, double q, double b, double x); 
    { 
     return kr * pow(x,4.0) + uc * x - q - pow(velocity/b, 2.0) * sin(radians); 

    }} 
    } 
+0

那麼這是編譯和工作的代碼?我看到的唯一x是函數fx()中的參數.... – jwismar 2011-05-11 04:30:43

+1

x在fx中聲明。它在哪裏拋出一個錯誤(或不)? – soandos 2011-05-11 04:31:05

+0

你能證明它的功能嗎? – 2011-05-11 04:31:30

回答

4
double fx(double radians,double velocity, double kr, double uc, double q, double b, double x); 

你有一個多餘的分號 「;」在行末。 :)

應該

double fx(double radians,double velocity, double kr, double uc, double q, double b, double x) 

你也有同樣的錯誤定義正割功能時。當你定義一個函數時,你不要在末尾加分號。

最終代碼應該是這個樣子

#include<iostream> 
#include<cmath> 
#include<iomanip> 
#include<fstream> 
// Declaration of functions used 
void secant(double, double, double, double, double, double, double, double, double&, int&); 
double fx(double, double, double, double, double, double, double); 
const double tol=0.0001; // Tolerance for convergence 
const int max_iter=50;  // Maximum iterations allowed 

using namespace std; // <- Goes after including the headers 

// main program 
int main() 
{ 
     int iteration;   // Number of iterations 

     double kr, uc, q, b, radians; 

     double x0, x1;   // Starting values for x 
     double root;   // Root found by secant method 
     const double PI = 4.0*atan(1.0); 
     ifstream datain ("shuttle.txt"); 
     ofstream dataout ("results.txt"); 
     datain >> kr >> uc >> q >> b; 
     int velocity = 16000; 
     double angle =10; 
     x0= 1000; 
     x1 = 200; 
     for (int velocity = 16000; velocity <= 17500; velocity += 500) 
     { 
      for (int angle = 10; angle <= 70; angle += 15) 
     { 
      radians= angle * PI/180 ; 
        cout << velocity << endl; 
        cout << radians << endl; 
        cout << angle << endl; 
        secant (radians, velocity, kr, uc, q, b, x0, x1, root, iteration); 
     } 
    } 

    // Output 
     cout<<"\nThe root is = "<<root<<endl; 
     cout<<"The number of iterations was = "<<iteration<<endl; 
     cout<<"The value of f(x) at the root = "<<fx(radians, velocity, kr, uc, q, b, root)<<endl<<endl; 
     system("pause"); 
    return 0; 
} 


// Definition of function "secant" 
// Receives a, b, c, d and x0 values from main program 
// Returns root and the iterations required 
void secant(double radians,double velocity, double kr, double uc, double q, double b, double x0, double x1, double& root, int& iteration) 
{ 
     double xnminus1, xnplus1, xn; // Local variables 
     iteration=0;     // Initialize iterations 
     xnminus1=x0; 
     xn=x1; 
     do 
     { 
      ++iteration; 
      xnplus1 = xn - fx(radians, velocity, kr, uc, q, b, xn)*(xn-xnminus1)/ 
             (fx(radians, velocity, kr, uc, q, b, xn)-fx(radians, velocity, kr, uc, q, b, xnminus1)); 
      cout<<"x"<<iteration+1<<" = "<<xnplus1<<endl; 
      xnminus1 = xn; 
      xn=xnplus1; 
     } 
     while ((fabs(fx(radians, velocity, kr, uc, q, b, xnplus1)) >= tol)&& (iteration < max_iter)); 
     root=xnplus1; 
} 


// Defines "fx" -- calculates f(x) = ax^3+bx^2+cx+d 
double fx(double radians,double velocity, double kr, double uc, double q, double b, double x) 
{ 
     return kr * pow(x,4.0) + uc * x - q - pow(velocity/b, 2.0) * sin(radians); 

} 
+0

我加了;因爲我收到一個錯誤,說「預期;在返回之前」和「在函數定義不允許在這裏返回之前」,並解決了錯誤 – Brian 2011-05-11 04:45:06

+0

@Brian這是因爲你沒有關閉for循環正確的括號「}」和編譯器認爲你正試圖在循環內定義函數。 – Pepe 2011-05-11 04:46:57

+0

我將如何改變它以正確關閉它? – Brian 2011-05-11 04:50:30

0

我假設你指的是在功能fxx變量:

x被聲明爲參數,因此爲什麼它可以在不使用一個顯式的局部變量聲明。什麼可能會丟你的是fx的前向聲明沒有得到它的參數名稱

+0

不在我的程序中工作?我遵循相同的佈局? – Brian 2011-05-11 04:40:52