Guest User

Untitled

a guest
Jan 18th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <errno.h>
  6.  
  7. #define SIZE 16
  8.  
  9. int main(int argc, char *argv[])
  10. {
  11. int data[SIZE] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
  12.  
  13. if (argc != 2)
  14. {
  15. printf("Not the correct number of arguments\n");
  16. }
  17. else
  18. {
  19. printf("Answer: %d\n", search(data, 2, 0, 15));
  20. }
  21. return 0;
  22. }
  23.  
  24. int search(int a[], int find, int begin, int end)
  25. {
  26. if(begin == end)
  27. {
  28. printf("%d, %d, %d\n", find, begin ,end);
  29. if(*(a + end) != find)
  30. {
  31. exit(0);
  32. return end;
  33. }
  34. }
  35. else
  36. {
  37. pid_t child = fork();
  38.  
  39. if(child == 0)
  40. {
  41. return search(a, find, begin, (begin + end)/2);
  42. }
  43. else
  44. {
  45. return search(a, find, (begin + end)/2 + 1, end);
  46. }
  47. }
  48. }
Add Comment
Please, Sign In to add comment