fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void foo(float x, int n, float *v)
  5. {
  6. int i, j;
  7. *v = 2.0f;
  8. printf("2 ");
  9. for (i=0; i<=n; ++i)
  10. {
  11. float element = 1.0f;
  12. for(j=1; j<=i*2+1; ++j)
  13. element *= x/j;
  14. printf("%c %.7f ", '+' + i%2*2, element);
  15. *v += (i%2 ? -element : element);
  16. }
  17. printf("\n");
  18. }
  19.  
  20. int main()
  21. {
  22. float x1, x2, v1,v2;
  23. x1 = 1, x2 = 2;
  24. int n = 5;
  25. printf("\n");
  26. foo(x1,n,&v1);
  27. foo(x2,n,&v2);
  28. printf(" v1 = %f \n\n", v1);
  29. printf(" v2 = %f \n\n", v2);
  30. return(0);
  31. }
Success #stdin #stdout 0s 1788KB
stdin
Standard input is empty
stdout
2 + 1.0000000 - 0.1666667 + 0.0083333 - 0.0001984 + 0.0000028 - 0.0000000 
2 + 2.0000000 - 1.3333333 + 0.2666667 - 0.0253968 + 0.0014109 - 0.0000513 
 v1 = 2.841471 

 v2 = 2.909296