C. *_*Ben -1 c recursion if-statement
我正在研究如何编程,并且最近一直在研究一个问题,该问题计算从最小到最大的2个输入数字的总和.例如,如果有人输入数字4,7.计算将是4 + 5 + 6 + 7 = 22.
我已经尝试了我认为recSum的定义,但显然它是错误的,因为我得到了一个分段错误.我的定义有什么问题?
/* Define the recursive function */
int recSum (int x, int max)
{
int incrementalSum = 0;
if (x == max)
{
return x; /* Exit if summated lower to upper numbers */
}
else
{
return (x + recSum(x++, max)); /* My recursive call */
}
} /* End of function call */
Run Code Online (Sandbox Code Playgroud)
*上面显示了新代码,抱歉,我使用了错误的代码.
您的代码有3个重要问题
表达方式
incrementalSum = x + x++;
Run Code Online (Sandbox Code Playgroud)
未定义,请阅读此内容以获取更多信息
你的函数不是递归的,递归函数会调用它自己,直到条件发生它应该结束.
此外,注意到我不是一个非理性的" 不要使用goto人 ",这正是有些人反对使用的建议goto.