我在使用數組進行遞歸時有點困惑,任何人都可以糾正我的錯誤嗎?計算雙數組中所有元素的總和
新的更新,基於問題需要一些行不能編輯
double sum_of_array(double x[],int size)
{
static double sum; <---can be edit
int index = 0; <--can be edit
if(index<size){
return sum + sum_of_array(x,size-1); <--can be edit
}
else {
something ; <--can be edit
return sum; <--can be edit
}
}
int main(void){
double x[] = {4.5,5.0,6.8};
double y[] = {4.7,3.4,2.5,5.2};
cout<<"Sum X = "<<sum_of_array(x,3)<<endl;
cout<<"Sum Y = "<<sum_of_array(y,4)<<endl;
return 0;
}
輸出:
Sum of the element in X[]=15.3
Sum of the element in Y[]= 15.8
BTW:'4.5 + 5.0 + 6.8 == 16.3!= 15.3' – quasiverse
投票結束爲「爲什麼不工作」。 –