Advertisement
kanciastopantalones

Untitled

May 11th, 2015
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 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 calka(double from, double to)
  15. {
  16. double ile_f = (to - from) / n;
  17.  
  18.  
  19. //zaokraglanie w gore i rzutowanie na int
  20.  
  21.  
  22. int ile = ile_f > 0 ? (int)(ile_f + 0.9) : -(int)(ile_f - 0.9);
  23.  
  24. int i;
  25.  
  26. double podsuma = 0;
  27.  
  28. double x = from, y = from + n;
  29.  
  30. printf("Od X:%f\t Do X:%f\t ile:%d\t ile_f:%f\n", from, to, ile, ile_f);
  31. for (i=0; i<ile; i++)
  32. {
  33. podsuma += (fun(x) + fun(y)) * n /2.0;
  34. x = y;
  35. y += n;
  36. }
  37.  
  38. return podsuma;
  39. }
  40.  
  41. int main(int argc, char* argv[])
  42. {
  43.  
  44.  
  45. if(argc < 2)
  46. {
  47. printf("za malo parametrow");
  48. return 1;
  49.  
  50. }
  51.  
  52.  
  53. n = atof(argv[1]);
  54. int k = atoi(argv[2]);
  55. double a, b, suma;
  56.  
  57. printf("\n%s: %f, %s: %d\n\n", "krok(n)", n, "l.proc.potom(k)", k);
  58.  
  59. int i, potok[2];
  60. pipe(potok);
  61. double skok = 30.0/k;
  62. double from = -15.0;
  63.  
  64. for(i=0;i<k;i++)
  65. {
  66. if(fork() == 0)
  67. {
  68. a = calka(from, from+skok);
  69. write(potok[1], &a, sizeof(a));
  70. exit(0);
  71.  
  72. }
  73. from += skok;
  74. }
  75.  
  76. suma = 0.0;
  77. for(i=0;i<k;i++)
  78. {
  79. read(potok[0], &b, sizeof(b));
  80. suma += b;
  81. }
  82.  
  83. printf("\nsuma = %f\n", suma);
  84.  
  85. exit(0);
  86. return 0;
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement