C++;精确求平方根 这是我第一次使用C++,我的作业如下,尝试如下: 你要写一个程序,将计算平方根到所需的精度。通过提示用户输入两个数字开始程序:(1)用于确定平方根的值和(2)结果中所需精度的小数位数。使用循环分别确定下一个最高完美平方和下一个最低完美平方。然后应构造计数控制回路;它将为每个所需的十进制位置执行一次 在单次通过该计数控制回路期间,近似值将增加或减少Δ=1/10decimalPosition,直到通过实际值。回路第一次通过时,Δ将为1/101=0.1;第二次通过时,Δ将为1/102=0.01,依此类推。如果近似值太小,计数控制循环内的事件控制循环将增加近似值,直到它变得太大
我不确定如何开始为第2段中的流程构建循环。尽管 这些指示对我来说毫无意义 我目前的进展:C++;精确求平方根 这是我第一次使用C++,我的作业如下,尝试如下: 你要写一个程序,将计算平方根到所需的精度。通过提示用户输入两个数字开始程序:(1)用于确定平方根的值和(2)结果中所需精度的小数位数。使用循环分别确定下一个最高完美平方和下一个最低完美平方。然后应构造计数控制回路;它将为每个所需的十进制位置执行一次 在单次通过该计数控制回路期间,近似值将增加或减少Δ=1/10decimalPosition,直到通过实际值。回路第一次通过时,Δ将为1/101=0.1;第二次通过时,Δ将为1/102=0.01,依此类推。如果近似值太小,计数控制循环内的事件控制循环将增加近似值,直到它变得太大,c++,C++,我不确定如何开始为第2段中的流程构建循环。尽管 这些指示对我来说毫无意义 我目前的进展: int sqroot() { cout << setiosflags (ios::fixed|ios::showpoint); //assumed I need for output later double number; cout<<"Enter the number to find the square root of: "; cin>
int
sqroot()
{
cout << setiosflags (ios::fixed|ios::showpoint); //assumed I need for output later
double number;
cout<<"Enter the number to find the square root of: ";
cin>>number;
int sqRoot = 1;
while (sqRoot*sqRoot < number) // sqRoot is too small
sqRoot++; // try the next number
int y=0;
int newRoot =1;
while (y < number)
{
if (newRoot*newRoot > number )
break;
y=newRoot*newRoot;
newRoot++;
}
int decimalInput;
int decimalPosition= 0;
cout<<"Enter the desired decimal position: "<<endl;
cin>>decimalInput;
while (decimalPosition < decimalInput)
decimalPosition++;
return 0;
}
int
sqroot()
{
电话号码)
打破
y=新根*新根;
newRoot++;
}
整数分贝输入;
int-decimalPosition=0;
cout您不应该仅仅将newRoot增加1(newRoot++),而是通过更小的步骤。为了提高效率,你从大步开始(10的大幂,例如1000)。在结果的平方大于输入数字之前,将这一步除以10
额外提示:
有两个嵌套循环。外部循环检查步骤>=错误,内部循环检查结果是否足够小。如果确实需要A,请添加相对错误检查此引用