Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this questionPlease help. My program is not working.
It should take N from the input of the user and calculate the function of nested e. For example
N=4
e=2+1/2(1+1/3(1+1/4))#include <stdio.h>
#include <stdlib.h>
double nested_e(int N)
{
double e, factor_e, a;
int n;
if(N==1)
e=2;
else if(N==2)
e=2+0.5;
else
{
a=1;
for(n=2;n<N;n++)
{
factor_e = a * 1/N +1;
a = factor_e;
N--; 开发者_如何学C
}
e = factor_e + 2 ;
}
printf("%lf", e);
return 0;
}
int main(void)
{
int s;
printf("Enter the number : \n");
scanf("%d", &s);
double nested_e(s);
return 0;
}
I suggest you turn your logic inside out. Instead of starting at the 2 +
, (increasing n
) start at the (1 + 1/4)
and work your way out (decreasing n
).
This line is very wrong:
double nested_e(s);
If you want to call existing function, you need no to specify its return type.
So, change this line to
nested_e(s);
I see many things wrong, but this is homework so... you should not have special cases for N==1 and N==2, they are both not special in any way so your algorithm could be more general. Think a little bit harder and you'll get it!
There are tools to help you in this kind of situation. Start by placing printfs
everywhere so you understand what is going on. If you know how to do it, you could also use a debugger
. Then you can also try to add an option for more warnings to the compiler.
精彩评论