Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <unistd.h>
  4.  
  5. using namespace std;
  6.  
  7. int count_occurrences(int num, int arr_size, int arr[]);
  8.  
  9. int main()
  10. {
  11. //set up random integer array
  12. int array_size = 1000;
  13. int array[array_size];
  14.  
  15. for (int i=0; i<array_size; i++)
  16. {
  17. array[i] = rand() % 101;
  18. }
  19.  
  20. //instead of reading in input file, just add this array
  21. int nums_to_check[19] = {5,13,24,6,17,20,1,51,36,42,2,19,67,35,64,91,96,84,72};
  22.  
  23.  
  24. //stream through the input file
  25. for (int i=0; i<19; i++)
  26. {
  27. int num_occ;
  28. int fork_check;
  29.  
  30. //fork child process
  31. fork_check = fork();
  32.  
  33. // check if it is child process
  34. if (fork_check == 0)
  35. {
  36. num_occ = count_occurrences(nums_to_check[i], 1000, array);
  37. cout << "query: " << a << "\tcount: " << num_occ << "\tPID: " << pid << endl;
  38. return 0;
  39. }
  40.  
  41.  
  42. return 0;
  43. }
  44.  
  45. int count_occurences(int num, int arr_size, int arr[])
  46. {
  47. int count = 0;
  48.  
  49. for (int i=0; i<arr_size; i++)
  50. {
  51. if (arr[i] == num)
  52. {
  53. count += 1;
  54. }
  55. }
  56. return num;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement