Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <sys/types.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <errno.h>
- #define SIZE 16
- int main(int argc, char *argv[])
- {
- int data[SIZE] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
- if (argc != 2)
- {
- printf("Not the correct number of arguments\n");
- }
- else
- {
- printf("Answer: %d\n", search(data, 2, 0, 15));
- }
- return 0;
- }
- int search(int a[], int find, int begin, int end)
- {
- if(begin == end)
- {
- printf("%d, %d, %d\n", find, begin ,end);
- if(*(a + end) != find)
- {
- exit(0);
- return end;
- }
- }
- else
- {
- pid_t child = fork();
- if(child == 0)
- {
- return search(a, find, begin, (begin + end)/2);
- }
- else
- {
- return search(a, find, (begin + end)/2 + 1, end);
- }
- }
- }
Add Comment
Please, Sign In to add comment