-2
我跑這個代碼只有我的zybook類,它給了我錯誤的答案,並告訴我使用浮點數的函數。我如何使用平方函數的浮點數。使用浮點數函數
編輯:
遺憾的混亂
#include <iostream>
#include <string>
using namespace std;
float squareRoot(float s) {
float xn;
if (s == 0) {
xn = 0;
}
else {
xn = s/2.0;
int counter = 1;
while (counter <= 10) {
xn = (xn + (s/ xn))/2.0;
counter = counter + 1;
}
}
return xn;
} int square(int what) {
return what* what;
}
int main() {
float sideA, sideB, sideC;
string answer = "yes";
while (answer == "yes") {
cout <<" Enter side A: ";
cin >> sideA;
cout << sideA << endl;
cout <<" Enter side B: ";
cin >> sideB;
cout << sideB << endl;
sideC = squareRoot(square(sideA) + square(sideB));
cout <<"Side C is " << sideC << endl;
cout <<"Continue? ";
cin >> answer;
cout << answer << endl;
}
return 0;
}
什麼? 「浮動功能」你是什麼意思? – tkausl
「浮動功能」是什麼意思? – user463035818
鑑於示例代碼沒有顯示sideA和sideB的類型,我們如何判斷? –