The first trick is to tidy the code.
1. k was defined and set, but never used. Let's just remove it
2. j is defined as a float universally and then as an int locally in main, remove that and only define locally. Since you only use it in the function, just define it there.
3. In c++ we ussually prefer to use cin or cout instead of printf and scanf.
4. void evaluate (float b) only outputs a printf, it would be much more useful if it returned our result. Make it a float function instead and return b.
5. You don't need the printf in line 23, so to make it readable lets remove it.
6. The last 14 lines of commented code is irrelevant, lets remove it.
7. Indents!
We are left with this: Much easier to read and therefore it will be easier for you to spot your problem:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
float sine ( float x )
{
float p=1, s=0;
for (int i=0;i<=100000; i++)
{
for (int j = 1; j<=(2*i+1); j++)
p *= x/(float)j;
if (i%2==0) s += p;
else s -= p;
}
return s;
}
|