fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. float calculafx(float x2, float *f3, float *f4, int m) {
  6. float fx1 = 0;
  7. for (int j = 1; j <= m; j++) fx1 += f3[j] * pow(x2, f4[j]);
  8. return fx1;
  9. }
  10.  
  11. int main() {
  12. int n;
  13. float epsilon, fa, fb, x1, fxx1, a, b;
  14. printf("Digite o número de termos da função: \n");
  15. scanf("%d", &n);
  16. system("clear");
  17. float f1[n], f2[n];
  18. printf("Digite o epsilon: \n");
  19. scanf("%f", &epsilon);
  20. printf("Digite o primeiro valor do intervalo: \n");
  21. scanf("%f", &a);
  22. printf("Digite o segundo valor do intervalo: \n");
  23. scanf("%f", &b);
  24. system("clear");
  25. for(int i = 1; i <= n; i++) {
  26. printf("Digite o termo %d da função:\n", i);
  27. scanf("%f", &f1[i]);
  28. printf("Digite o grau do termo: \n");
  29. printf("Digite 0 se o termo for uma constante:\n");
  30. scanf("%f", &f2[i]);
  31. system("clear");
  32. }
  33.  
  34. do {
  35. x1 = (a + b) / 2;
  36. fxx1 = calculafx(x1, f1, f2, n);
  37. if (fxx1 * calculafx(a, f1, f2, n) < 0) b = x1;
  38. else a = x1;
  39. } while (abs(a - b) > epsilon);
  40. fa = calculafx(a, f1, f2, n);
  41. fb = calculafx(b, f1, f2, n);
  42. printf("f(a) é igual a %f e f(b) é igual a %f \n", fa,fb);
  43. }
  44.  
  45. //https://pt.stackoverflow.com/q/185068/101
Success #stdin #stdout #stderr 0s 4436KB
stdin
Standard input is empty
stdout
Digite o número de termos da função: 
Digite o epsilon: 
Digite o primeiro valor do intervalo: 
Digite o segundo valor do intervalo: 
f(a) é igual a 0.000000 e f(b) é igual a 0.000000 
stderr
TERM environment variable not set.
TERM environment variable not set.