平方根递归

时间:2015-05-21 23:32:14

标签: c++

我试图创建一个递归方法,该方法将显示从1到指定最大数字n的平方数字序列。演示的格式将首先显示奇数的平方序列,从最大的奇数(< = n)开始到最小的奇数(1),然后显示偶数的平方,从最小的开始偶数(2)到最大偶数(< = n)。我收到一条错误消息,指出未初始化的本地变量' upperB'正在使用。有什么想法吗?

 #include "stdafx.h" 
 #include <iostream> 
 #include <iomanip>
 #include <math.h>\
using namespace std; 
int main()
//Input a,b are constants,lower and upper approximation points as well as 
//precision value N   
{ double a, b, N, lowerB, upperB;
cout << "Give me a value for a: ";
cin >> a;
cout << "Give me a value for b: ";
cin >> b;
cout << "Give me a precision :";
cin >> N;
cout << " Give me  lower and upper approximations: ";
cin >> lowerB, upperB;
cout << "The root is : " <<
RootFinderSMNEW(a, b,  lowerB, upperB, N) << endl;
system("pause");
return 0;
    }

double f( double x, double a, double b)
{
    return sin((a* x) / (1 + x*x))*atan(b*x) + atan(x);
}

double RootFinderSMNEW(double a, double b, double lowerB, double upperB, int N)
{
    double f_left = f(lowerB, a, b);
    double now = lowerB + N;
    double f_right = f(now, a, b);
    while (f_left * f_right > 0 && now < lowerB)
    {
        f_left = f_right;
        now += N;
        f_right = f(now, a, b);
    }
    return now - N / 2;
}

1 个答案:

答案 0 :(得分:4)

你的意思是

cin >> lowerB >> upperB;

cin >> lowerB, upperB;

您正在做的不是将值放入upperB,因此未初始化。这正是错误信息所说的内容。