Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <math.h>
  5. #include <time.h>
  6. #include <sys/wait.h>
  7. #include <unistd.h>
  8.  
  9.  
  10. const int k = 10;
  11. const int N = 5;
  12. int sumOfTerms = 0;
  13.  
  14. int fillArray(int *arr){
  15. for (int i = 0;i<k;i++){
  16. arr[i] = rand()%20;
  17. }
  18. }
  19.  
  20. void printArray(int *arr){
  21. for(int i=0;i<k;i++){
  22. // printf("%d ",arr[i]);
  23.  
  24. }
  25. }
  26.  
  27. int findMax(int *arr){
  28. int max=arr[0];
  29. for(int i = 0; i<k;i++){
  30. if(max<arr[i]){
  31. max = arr[i];
  32. }
  33. }
  34. return max;
  35. }
  36. int fillSeriesArray(int *arr, int size){
  37. arr[0] = 12;
  38. arr[1] = -8;
  39. arr[2] = 6;
  40.  
  41. for(int i=3;i<size;i++){
  42. arr[i] = -8 * arr[i-1] - sqrt(2/3 * arr[i-2]) + 13 * arr[i-3];
  43. }
  44.  
  45. }
  46.  
  47.  
  48.  
  49. int main(){
  50.  
  51. srand(time(NULL));
  52. int max;
  53. int *randomNumbers = (int*)malloc(sizeof(int)* k);
  54.  
  55.  
  56. fillArray(randomNumbers);
  57. printArray(randomNumbers);
  58.  
  59. max = findMax(randomNumbers);
  60. int* seriesArray=(int*)malloc(max*sizeof(int));
  61. fillSeriesArray(seriesArray,max);
  62. printArray(seriesArray);
  63.  
  64. int input[2], output[2];
  65. pipe(input);
  66. pipe(output);
  67. int i = 0;
  68.  
  69.  
  70. // proces nadrzedny wysyla liczbe z tablicy do obliczenia
  71. for(i =0;i< k;i++){
  72. write(input[1],&randomNumbers[i],sizeof(randomNumbers[i]));
  73. }
  74.  
  75. for(i = 0;i<N;i++){
  76. if(fork() == 0){
  77.  
  78. while(1){
  79. int term = 0;
  80. read(input[0],&term, sizeof(term));
  81. if(term<0)
  82. exit(0);
  83.  
  84. int result = seriesArray[term];
  85. write(output[1],&result,sizeof(result));
  86. }
  87. }
  88. }
  89.  
  90. //
  91. for(i = 0; i<N;i++){
  92. int result = 0;
  93. read(output[0],&result,sizeof(result));
  94. sumOfTerms+=result;
  95. }
  96.  
  97. for(i = 0; i<N;i++){
  98. int terminate = -1;
  99. write(input[1],&terminate,sizeof(terminate));
  100. }
  101.  
  102. printf("Suma wszystkich wyrazów wynosi: %d",sumOfTerms);
  103. return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement