The main problems seems to be that the for loop in your factorial function doesn't do anything.
1 2 3 4 5
double factorial(double x) {
double p = 1.0;
for (double i = 1.0; i <= x; i++)
return p *= i;
}
is (almost) the same as
1 2 3 4
double factorial(double x) {
double p = 1.0;
return p *= 1.0;
}
and if x happens to be below 1.0 you'll get garbage or a crash because it won't execute the for loop once and therefore won't even call a return.
Do you see how the for loop is basically useless in its current state?
Think about where you want to return in your function, and what you want to do before you return.
hint: n! = n*(n-1)! AKA factorial(n) == n*factorial(n-1)