Advertisement
Guest User

Untitled

a guest
May 30th, 2016
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. //Bartosz Bojda gr. GKiO2
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <math.h>
  6.  
  7. float calka(float x1, float x2, float n)
  8. {
  9. float suma = 0;
  10. while (x1 < x2)
  11. {
  12. float wynik1 = 8 * x1 * x1 * x1 * x1 * x1 - 2 * x1 * x1 * x1 * x1 + 15 * x1 * x1 - 24 * x1 + 18;
  13. x1 += n;
  14. float wynik2 = 8 * x1 * x1 * x1 * x1 * x1 - 2 * x1 * x1 * x1 * x1 + 15 * x1 * x1 - 24 * x1 + 18;
  15.  
  16. /*if ( wynik1 >= wynik2)
  17. {
  18. suma += (n * wynik2) + ((wynik1 - wynik2) * n / 2);
  19. }
  20. else
  21. {
  22. suma += (n * wynik1) + ((wynik2 - wynik1) * n / 2);
  23. }*/
  24. suma += (wynik1 + wynik2) * n / 2;
  25. }
  26. return suma;
  27. }
  28.  
  29. int main(int argc, char *argv[])
  30. {
  31. float n;
  32. int k;
  33. float rangeAdd;
  34. float x1, x2;
  35. float wynik = 0;
  36. float readValue;
  37.  
  38. n = atof(argv[1]);
  39. k = atoi(argv[2]);
  40.  
  41. printf("n = %f\n", n);
  42. printf("k = %d\n", k);
  43.  
  44. rangeAdd = 30 / k;
  45.  
  46. int pipeTable[2];
  47. pid_t processID;
  48.  
  49. pipe(pipeTable);
  50.  
  51. x1 = -15;
  52. int i;
  53.  
  54. for (i = 0; i < k; ++i)
  55. {
  56. if (i != 0)
  57. {
  58. x1 += rangeAdd;
  59. }
  60. x2 = x1 + rangeAdd - n;
  61. processID = fork();
  62.  
  63. if (processID == 0)
  64. {
  65. wynik = calka(x1, x2, n);
  66. write(pipeTable[1], &wynik, sizeof(wynik));
  67. exit(0);
  68. }
  69. }
  70.  
  71. for (i = 0; i < k; ++i)
  72. {
  73. read(pipeTable[0], &readValue, sizeof(readValue));
  74. wynik += readValue;
  75. }
  76.  
  77. printf("wynik = %f\n", wynik);
  78.  
  79. exit(0);
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement