2016-11-13 90 views
-2

我跑這個代碼只有我的zybook類,它給了我錯誤的答案,並告訴我使用浮點數的函數。我如何使用平方函數的浮點數。使用浮點數函數

This is the image for the output results

編輯:

遺憾的混亂

#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; 
     } 
+0

什麼? 「浮動功能」你是什麼意思? – tkausl

+0

「浮動功能」是什麼意思? – user463035818

+0

鑑於示例代碼沒有顯示sideA和sideB的類型,我們如何判斷? –

回答

1

您的功能:

int square (int what){ 
return what * what; 
} 

應改爲返回浮動。當你將浮點數傳遞給這個函數時,它將它們作爲整數返回,而不是3.3和4.4取平方,3和4做。

您應該返回一個浮點數並將其更改爲浮點數。