[問題] 我的副函數無法傳值到主函數

作者: eggsu (數學一等兵)   2015-07-13 05:47:40
開發平台(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):
謝謝大家指教!
作者: putumaxally (putumaxally)   2015-07-13 05:50:00
else 沒 return ?
作者: eggsu (數學一等兵)   2015-07-13 05:52:00
else 不 return 啊……還沒有找到值,繼續找我懂了。試試!
作者: putumaxally (putumaxally)   2015-07-13 05:54:00
遞迴一樣要return
作者: eggsu (數學一等兵)   2015-07-13 06:06:00
解決了,感恩!

Links booklink

Contact Us: admin [ a t ] ucptt.com