fork download
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5.  
  6. typedef double (*function)(double);
  7.  
  8. double* tabl_func(function f, double xmin, double xmax, double dx)
  9. {
  10.  
  11. int i = 0;
  12. double sizemas;
  13. sizemas = ((xmax - xmin) / dx) + 1;
  14. double *arry = (double*)calloc(sizemas, sizeof(double));
  15. for (i=0;i<sizemas;i++)
  16. {
  17. xmin += dx;
  18. arry[i] = f(xmin);
  19. }
  20. return arry;
  21. }
  22.  
  23. double Y(double x) { return 1 - x; }
  24.  
  25. void print_res(int size, double* arry)//функция печати
  26. {
  27. printf("| f(x)|\n");
  28. for (int i = 0; i < size; i++)
  29. {
  30. printf("--------------------\n");
  31. printf("| %5.5lf |\n", arry[i]);
  32. }
  33. printf("\n");
  34. }
  35.  
  36. int main(int argc, const char * argv[])
  37. {
  38. double* arry;
  39. // arry = (double*)calloc(sizemas, sizeof(double)); // Утечка!!
  40. arry = tabl_func(Y, 0, 1, 0.1);
  41.  
  42. print_res(11,arry);
  43.  
  44. }
  45.  
Success #stdin #stdout 0s 4528KB
stdin
Standard input is empty
stdout
| f(x)|
--------------------
| 0.90000 |
--------------------
| 0.80000 |
--------------------
| 0.70000 |
--------------------
| 0.60000 |
--------------------
| 0.50000 |
--------------------
| 0.40000 |
--------------------
| 0.30000 |
--------------------
| 0.20000 |
--------------------
| 0.10000 |
--------------------
| 0.00000 |
--------------------
| -0.10000 |