[英]C++ Finding square roots to a precision
這是我第一次使用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>>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;
}
您不僅應該將新的newRoot遞增1(newRoot ++),而且應以較小的步長遞增。 為了提高效率,您可以開始大步(10的冪次方,例如1000)。在結果的平方大於輸入數之前,將該步除以10。
額外提示:
有兩個嵌套循環。 外循環檢查步驟> =錯誤,內循環檢查結果是否足夠小。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.