gpt4 book ai didi

c++ - 平方根递归

转载 作者:行者123 更新时间:2023-11-27 22:58:17 24 4
gpt4 key购买 nike

我正在尝试创建一个递归方法,该方法将显示从 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;
}

最佳答案

你的意思是

cin >> lowerB >> upperB;

不是

cin >> lowerB, upperB;

您所做的并不是将值放入 upperB,因此它未被初始化。这正是错误消息所说的。

关于c++ - 平方根递归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30386062/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com