Reynevan - 8 months ago 33

C Question

I have these two equations

and I need to convert them to C code where you input

`k`

`x`

Can anyone show me step by step what built-in functions can be used for this and how exactly should the logic behind the app work?

Cheers!

Answer

Your formula is wrong. As shown here (along with the proof of the derivation) the correct formula is

You have `k`

and `n`

swapped in your summation. The inputs should then be `x`

and `n`

. The correct code is then:

```
#include <math.h>
double sum_of_sin(double x, int n) {
if (sin(x/2) == 0.0) {
return 0.0; //prevent division by 0 for x multiple of 2π
}
return sin(n*x/2) * sin((n+1)*x/2) / sin(x/2);
}
```

Source (Stackoverflow)