Advertisement
kanciastopantalones

Untitled

May 11th, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. // Robert Sokolowski
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <math.h>
  6.  
  7. double n;
  8.  
  9. double fun(double x)
  10. {
  11. return 8.0*pow(x,5) - 2.0*pow(x,4) + 15*x*x - 24.0*x + 18.0;
  12. }
  13.  
  14. double fragment(double from, double to)
  15. {
  16. double ile_f = (to - from) / n;
  17.  
  18.  
  19. int ile = ile_f > 0 ? (int)(ile_f + 0.9) : -(int)(ile_f - 0.9);
  20.  
  21. int i;
  22.  
  23. double calka = 0;
  24.  
  25. double x = from, y = from + n;
  26.  
  27. printf("Od X:%f\t Do X:%f\t ile:%d\t ile_f:%f\n", from, to, ile, ile_f);
  28. for (i=0; i<ile; i++)
  29. {
  30. calka += (fun(x) + fun(y)) * n /2.0;
  31. x = y;
  32. y += n;
  33. }
  34.  
  35. return calka;
  36. }
  37.  
  38. int main(int argc, char* argv[])
  39. {
  40.  
  41. int potok[2];
  42.  
  43. double skok, from, pole;
  44.  
  45. if(argc < 2)
  46.  
  47. {
  48. printf("Podales za malo parametrow");
  49.  
  50. return 1;
  51.  
  52.  
  53. }
  54.  
  55.  
  56. n = atof(argv[1]);
  57. int k = atoi(argv[2]);
  58. double a, b;
  59.  
  60. printf("\n%s: %f, %s: %d\n\n", "krok(n)", n, "l.proc.potom(k)", k);
  61.  
  62. int i;
  63. pipe(potok);
  64.  
  65.  
  66.  
  67. skok = 30.0/k;
  68. from = -15.0;
  69.  
  70.  
  71. for(i=0;i<k;i++)
  72. {
  73. if(fork() == 0)
  74. {
  75. a = fragment(from, from+skok);
  76. write(potok[1], &a, sizeof(a));
  77.  
  78.  
  79. exit(0);
  80.  
  81. }
  82. from += skok;
  83. }
  84.  
  85. pole = 0.0;
  86.  
  87. for(i=0;i<k;i++)
  88. {
  89.  
  90. read(potok[0], &b, sizeof(b));
  91. pole += b;
  92. }
  93.  
  94. printf("\nPole = %f\n", pole);
  95.  
  96.  
  97. exit(0);
  98. return 0;
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement