### linear and binary search problem

these two method can only search the first element you want to find, but if there's 2 or more same elements you want to find, and store them, what should I do? Am I right think that?
 ``1234567891011121314151617`` `````` void binarySearch(int array[], int size, int key) { int low = 0; int high = size - 1; int mid; // how I can create an array to store the result? it's size is what? while(low <= high) { mid = (high-low)/2; if(key == array[mid]) // what should I do?? else if(key > array[mid]) low = mid + 1; else high = mid - 1; } }``````
You may pass a second array:
 ``1234567891011121314151617181920212223`` ``````void binarySearch(int result_array[], int *result_size, int array[], int size, int key) { int r_size = (*result_size); // On input: You need to know how many items can be stored (*result_size) = 0; // On output: How many items you actually did store int low = 0; int high = size - 1; int mid; // how I can create an array to store the result? it's size is what? while(low <= high) { mid = (high-low)/2; if(key == array[mid]) // what should I do?? { if((*result_size) < r_size) result_array[*result_size] = array[mid]; ++(*result_size); } else if(key > array[mid]) low = mid + 1; else high = mid - 1; } }``````
Last edited on
 You need to know how many items can be stored

how could you know how many items to be stored?
the user input bunch of words, you couldn't know how many items equals` key`?
Topic archived. No new replies allowed.