[英]“Recursive on All Control Paths” error when implementing factorial function
For class I have an assignment: 对于上课我有一个任务:
Write a C++ program that will output the number of distinct ways in which you can pick
k
objects out of a set ofn
objects (bothn
andk
should be positive integers).编写一个C ++程序,它将输出一些不同的方法,你可以从一组
n
对象中选择k
n
对象(n
和k
都应该是正整数)。 This number is given by the following formula:该数字由以下公式给出:
C(n, k) = n!/(k! * (n - k)!)
Your program should use two value-returning functions.
您的程序应该使用两个值返回函数。 The first one should be called
factorial
and should returnn!
第一个应该被称为
factorial
,应该返回n!
.。 The second function should be called
combinations
and should returnn!/(k! * (n - k)!).
第二个函数应该被称为
combinations
,应该返回n!/(k! * (n - k)!).
Test your program for different values ofn
andk
five times (count-controlled loop).测试程序的
n
和k
不同值五次(计数控制循环)。
I came up with a solution: 我提出了一个解决方案:
#include <iostream>
using namespace std;
int factorial(int);
int combination(int, int);
void main(void)
{
int objects, set_number, count;
count = 1;
while(count <= 5)
{
cout << "Please enter in number of objects ";
cin >> objects;
cout << "Please enter in the number of Sets ";
cin >> set_number;
count++;
}
cout << "The Factorial is " << factorial(set_number) << " & the combination is " << combination << endl;
cout << endl;
}
// Factorial
int factorial(int set_number)
{
int cal;
cal = set_number * factorial(set_number - 1);
return cal;
}
// Combination
int combination(int objects, int set_number)
{
int com_total, cal_set, cal_obj, min_sum, cal_min;
cal_set = set_number * factorial(set_number - 1);
cal_obj = objects * factorial(objects - 1);
//n!/(k! * (n - k)!)
min_sum = set_number - objects;
cal_min = min_sum * factorial(min_sum- 1);
com_total = cal_set / (cal_obj * cal_min);
return com_total;
}
...but I keep getting an error, that says; ......但我不断收到错误,说;
"'factorial' : recursive on all control paths, function will cause runtime stack overflow;"
“'factorial':在所有控制路径上递归,函数将导致运行时堆栈溢出;”
If someone could help me, I've been working on this for about an hour and I'm stumped! 如果有人可以帮助我,我已经在这个工作了大约一个小时,我很难过!
There are two critical elements to a recursive function definition: 递归函数定义有两个关键元素:
You appear to be missing the termination condition. 您似乎错过了终止条件。 How would
factorial()
quit calling itself forever? factorial()
将如何退出自己永远?
You defined a recursive function (ie basically a function that calls itself), but you have not defined an exit condition. 您定义了一个递归函数(即基本上是一个调用自身的函数),但您还没有定义退出条件。 You are calling
factorial
again right before the return, so the function will never end, calling itself over and over again. 你在返回之前再次调用
factorial
,所以函数永远不会结束,一遍又一遍地调用自己。
You need to add a branch in there, ie 你需要在那里添加一个分支,即
if (set_number == 0)
{
return 1;
}
else
return set_number * factorial(set_number - 1);
You are missing a base case. 你错过了一个基本案例。 Factorial should return 1 for set_number <= 1
因为set_number <= 1,因此应该返回1
This function will result in infinitive recursion because it never stops calling itself: 此函数将导致不定式递归,因为它永远不会停止调用自身:
int factorial(int set_number)
{
int cal;
cal = set_number * factorial(set_number - 1);
return cal;
}
This is what you want: 这就是你想要的:
int factorial(int n)
{
if (n<=1)
return(1);
else
n=n*factorial(n-1);
return(n);
}
Your factorial function doesn't terminate on one, it just recurses indefinitely. 你的阶乘函数不会在一个函数上终止,它只是无限地递归。
int factorial(int set_number)
{
if (set_number <= 1)
return 1;
return set_number * factorial(set_number - 1);
}
Your coding style is also pretty poor, it looks very C-like. 你的编码风格也很差,看起来很像C。 There's no need for factorial and combination to be defined after main, and you declare all your vars at the top, no declarations and initializations mixed?
在main之后不需要定义阶乘和组合,并且您在顶部声明所有变量,没有声明和初始化混合?
Also, your main function doesn't actually do what the specification says it should - you never initialized or assigned to the combinations variable nor called the combination function, your variables are terribly named, etc. But this is your homework, not mine. 此外,您的主要功能实际上并没有按照规范所说的那样做 - 您从未初始化或分配给组合变量,也没有调用组合函数,您的变量非常命名等等。但这是您的作业,而不是我的。
int factorial(int set_number)
{
return set_number == 1?1:set_number * factorial(set_number - 1);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.