Im trying to get my factorial function to go through all the numbers in my array and give me the result but I keep getting an error message. Any idea what im doing wrong? Here is my code:
#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;
}
Your function is designed to take a single integer, and not an array. Iterate over the array, and call the method on each int within the array
for(int i = 0; i < 5; ++i)
{
int b = factorial(my_list[i]);
std::cout << b << std::endl;
}