我正在尝试让我的阶乘函数遍历数组中的所有数字并提供结果,但是我一直收到错误消息。知道我在做什么错吗?这是我的代码:
#include <iostream>
#include "Recursion.h"
int factorial(int n)
{
if (n == 0)
return 1;
else
return n * factorial(n-1);
}
int main()
{
int my_list[5] = {2, 4, 6, 8, 10};
for(int i = 0; i < 5; ++i)
{
int b = factorial(my_list[i]);
std::cout << b << std::endl;
}
return 0;
}
您的函数设计为采用单个整数,而不是数组。遍历数组,并在数组中的每个int上调用方法
for(int i = 0; i < 5; ++i)
{
int b = factorial(my_list[i]);
std::cout << b << std::endl;
}