fork download
  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. double rnd(void)
  7. {
  8. double d;
  9.  
  10. do {
  11. d = rand() - RAND_MAX / 2.0;
  12. } while (d == 0.0);
  13. return rand() / d;
  14. }
  15.  
  16. void f(double a, double b, double c, double x)
  17. {
  18. double f;
  19.  
  20. f = a * x * x + b * x + c;
  21. printf("x=%f f=%f\n", x, f);
  22. }
  23.  
  24. int main()
  25. {
  26. double a, b, c, d, x1, x2;
  27. int i;
  28.  
  29. srand(time(NULL));
  30. for (i = 0; i < 10; i++) {
  31. while (1) {
  32. a = rnd();
  33. b = rnd();
  34. c = rnd();
  35. d = b * b - 4 * a * c;
  36. if (a != 0.0 && d >= 0.0) break;
  37. }
  38. x1 = (-b + sqrt(d)) / (2 * a);
  39. x2 = (-b - sqrt(d)) / (2 * a);
  40. printf("a=%f b=%f c=%f\n", a, b, c);
  41. f(a, b, c, x1);
  42. f(a, b, c, x2);
  43. }
  44. return 0;
  45. }
  46.  
Success #stdin #stdout 0.02s 1720KB
stdin
Standard input is empty
stdout
a=7.044420 b=-1.471129 c=-5.209082
x=0.970654 f=-0.000000
x=-0.761818 f=-0.000000
a=-2.692180 b=8.392083 c=-4.691154
x=0.729909 f=0.000000
x=2.387298 f=0.000000
a=-1.246424 b=1.348647 c=2.189608
x=-0.890566 f=0.000000
x=1.972579 f=0.000000
a=-0.360990 b=1.333413 c=5.931059
x=-2.607436 f=0.000000
x=6.301206 f=0.000000
a=1.601657 b=15.306530 c=6.091433
x=-0.416078 f=0.000000
x=-9.140608 f=-0.000000
a=2.983542 b=-1.189518 c=-3.502951
x=1.301086 f=-0.000000
x=-0.902393 f=0.000000
a=-1.115473 b=-1.026956 c=2.324764
x=-1.975580 f=-0.000000
x=1.054934 f=0.000000
a=-1.559354 b=-1.238729 c=1.217885
x=-1.366101 f=-0.000000
x=0.571714 f=-0.000000
a=-1.639559 b=-3.076984 c=0.404592
x=-2.000094 f=-0.000000
x=0.123379 f=0.000000
a=-0.193361 b=-1.327085 c=-1.673938
x=-5.197690 f=-0.000000
x=-1.665559 f=-0.000000