2014-06-30 32 views
-1

我的教授在课堂上给了我们这段代码来展示一个程序如何工作,并说“回家试试看,你会看到它的作品”...... 30分钟后,我无法得到它跑步。有人可以帮助我,并指出我在正确的方向。谢谢!功能定义不允许/

-I得到在端“双克(双X)” 函数定义-On否则该第一其中x_left = x_mid控制到达非void函数的端

#include <stdlib.h> 
#include <stdio.h> 
#include <math.h> 


#define FALSE 0 
#define TRUE 1 
#define NO_ROOT -99999.0 

//function prototypes 

double bisect(double, double, double, double f(double farg)); 

// evaluation of function 

double g(double); 
double h(double); 

int main(void) { 

double x_left, x_right, epsilon, root; //declare variables 

// get endpoint and error tolerance 
printf("\nEnter interval endpoints > "); 
scanf("%lf%lf", &x_left, &x_right); 
printf("\nEnter tolerance > "); 
scanf("%lf", &epsilon); 

//use bisect function to look for roots of functions 
printf("\n\n For function g(x)"); 
root = bisect(x_left, x_right, epsilon, g); 
if (root != NO_ROOT) 
    printf("\n g(%.7f) = %e\n", root, g(root)); 

printf("\n\n For function h(x)"); 
root = bisect(x_left, x_right, epsilon, h); 
if (root != NO_ROOT) 
    printf("\n h(%.7f) = %e\n", root, h(root)); 

system("pause"); 
return (0); 

} 


// bisection method program coding 

double bisect(double x_left, double x_right, double epsilon, double f(double farg)){ 

double x_mid, f_left, f_right, f_mid; 
int root_found; 

// computes function at initial end points 
f_left = f(x_left); 
f_right = f(x_right); 

// if no change in sign 

if (f_left * f_right > 0) { 
    printf("\nmay not be no root in [%.7f, %.7f]\n\n", x_left, x_right); 
return NO_ROOT; 
} 

// searches as long as interval size is large enough 

root_found = FALSE; 

while (fabs(x_right - x_left) > epsilon && !root_found) { 

    // compute the mid point 
    x_mid = (x_left + x_right)/2.0; 
    f_mid = f(x_mid); 

    if (f_mid == 0.0) { 
     root_found = TRUE;} 
    else if (f_left * f_mid < 0.0) { 
     x_right = x_mid; 
    } else { 
     x_left = x_mid; 
    } 


// trace loop execution 
if (root_found) 
    printf("\nRoot found at x = %.7f , midpoint of [%.7f, %.7f] ", x_mid, x_leftx_right); 
else 
    printf("\nNew interval is [%.7f, %.7f] \n\n", x_left, x_right); 


//if there is a root 
return ((x_left + x_right)/2.0); 
} 

// functions for which roots are sought 

double g(double x){ 
return (5 * pow(x, 3.0) - 2 * pow(x, 2.0) +3); 
} 

double h(double x){ 
return (pow(x, 4.0) - 3 * pow(x,2.0) - 8); 
}; 
} 
+1

缺少}后'x_left = x_mid;}'' – chux

+0

双克(双)'是坏的,此心不是所述函数的函数原型或评估。 –

+0

@Ben'double g(double);'看起来像一个函数原型。 – chux

回答

1

我得到一个错误此行:

printf("\nRoot found at x = %.7f , midpoint of [%.7f, %.7f] ", x_mid, x_leftx_right 

说明x_leftx_right未申报。

如果我将其更改为x_left, x_right,那么除“未定义的引用g”和“未定义的引用h”外,它会编译“确定”。

对于g的未定义引用的原因是,您从未为功能g提供了原型为double g(double);的函数定义。您确实在bisect内提供了嵌套函数g。嵌套功能是非标准扩展,bisect::g是与g不同的功能。同样为h

若要解决此问题,请将gh的定义移至bisect函数结束之后;而不是在该功能中。

您的“控制达到非无效功能结束”警告的原因可能是因为while循环后没有return语句。

您的线路return ((x_left + x_right)/2.0);线路处于由while (fabs(x_right - x_left) > epsilon && !root_found) {开始的循环内。如果此循环通过循环条件结束不再为真,则执行到达函数的结尾而不返回任何内容。

注意:如果您正确缩进代码,以便排列{,那么您不太可能会遇到此类问题。您的编辑器应该有一个密钥,您可以使用它来查找匹配的大括号。另外,在严格标准模式下操作编译器会给出使用嵌套函数的错误。

`