作者eggsu (數學一等兵)
看板C_and_CPP
標題[問題] 我的副函數無法傳值到主函數
時間Mon Jul 13 05:47:40 2015
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
我是把筆電(windows7)上Dev C++裝好就寫的,不知道應該是算是那個平台
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
無
問題(Question):
副函數findroot應該能把平方根找到
但傳到主函數變數b時,總是得到0
餵入的資料(Input):
一個數
預期的正確結果(Expected Output):
例:輸入121,可以得到11,而不是0
錯誤結果(Wrong Output):
總是得到平方根是0
程式碼(Code):(請善用置底文網頁, 記得排版)
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
double findroot(double a, double c){
if(((a/c)-c)*((a/c)-c)<0.00000000001){
return c;
}
else {
findroot(a,((a/c)+c)/2);
}
}
int main(void){
//變數a,及其平方根b
double a,b;
//呈現使用說明
cout << "本程式將找出使用者給定之數的正平方根" << endl << "請輸入數字:" ;
cin >> a;
//計算平方根
b= findroot(a,1);
//顯示結果
cout << a << " 的正平方根為 " << b;
system("pause");
}
補充說明(Supplement):
謝謝大家指教!
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 49.158.107.184
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1436737662.A.AAF.html
※ 編輯: eggsu (49.158.107.184), 07/13/2015 05:50:01
→ putumaxally: else 沒 return ? 07/13 05:50
→ eggsu: else 不 return 啊……還沒有找到值,繼續找 07/13 05:52
→ eggsu: 我懂了。試試! 07/13 05:53
→ putumaxally: 遞迴一樣要return 07/13 05:54
→ eggsu: 解決了,感恩! 07/13 06:06