在 C++ 中的数组上使用阶乘函数
Using a Factorial Function on an Array in C++
我试图让我的阶乘函数遍历数组中的所有数字并给我结果,但我不断收到错误消息。知道我做错了什么吗?这是我的代码:
#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;
}
相关文章:
- 在 C++ 中的数组上使用阶乘函数
- 为什么我在C++阶乘函数中出现编译错误?
- 阶乘函数只返回C++中输入的答案
- 为什么我不能在非常量表达式上使用此模板阶乘函数?
- 返回阶乘C++的递归函数
- C++ 中是否有任何内置阶乘函数?
- 为什么叮当使我的简单阶乘函数过于复杂
- 3 函数加主函数 C 程序计算和显示阶乘
- 带有自定义数字的阶乘函数不起作用
- 阶乘函数不适用于大数
- 自定义阶乘函数未按预期工作
- 递归阶乘函数在"22!"上产生不正确的结果
- 为什么这个阶乘函数返回int而不是double
- 在C++中跟踪递归阶乘函数
- 带有值的递归阶乘错误返回语句,在函数中返回 'void' [-fallowive]
- 记忆化的递归阶乘函数
- 了解如何在c++中使用阶乘函数
- 为什么阶乘递归函数比普通阶乘函数效率低
- 有数据类型问题的c++阶乘函数
- 如何在c++中实现阶乘函数并进行正确的错误处理