ProgIng - Programación en Ingeniería
Loading...
Searching...
No Matches
Ejemplo016.c
Go to the documentation of this file.
1
35
36#include <stdio.h>
37
38int main(int argc, char *argv[])
39{
40 (void)argc;
41 (void)argv;
42
43 int n, i;
44 int signo; /* +1 o -1 alternando */
45 float x;
46 float sx; /* acumulador */
47 float fct; /* término positivo t_i = x^(2i+1)/(2i+1)! */
48
49 do {
50 printf("Ingrese el numero de terminos: ");
51 scanf("%d", &n);
52 } while (n < 1);
53
54 printf("Ingrese el valor de x: ");
55 scanf("%f", &x);
56
57 /*
58 Inicialización:
59 t0 = x
60 sx = 0
61 En cada iteración:
62 sx += (-1)^i * t_i
63 t_{i+1} = t_i * (x^2)/[(2i+2)(2i+3)]
64 */
65 for (i = 0, sx = 0, fct = x; i < n; i++)
66 {
67 signo = 1 - 2*(i % 2); /* +1 si i par; -1 si i impar */
68
69 sx += (signo * fct);
70
71 /* Actualizar al siguiente término positivo */
72 fct *= (x / (2*i + 2)) * (x / (2*i + 3));
73 }
74
75 printf("sen(%f) = %f\n", x, sx);
76 return 0;
77}
double fct(double x, int n)
Definition Ejemplo029.c:34
int main(void)
Definition Ejemplo_035.c:15