Hi people. Been doing some revision for an exam I have in a few months. To get a better idea i've been coding some search and sort algorithms. I have stumbled across a bug with my binary search and can't find it for the life of me! Could be trivial, but I would much appreciate some help. =]

Any questions about the code please don't hesistate to ask. Should take some input like '1234567' then say ask it to search for '5'. It should return a value of '4' as this is the index of its point in the array.

Thanks.

Code:#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> char search( char *A, int min, int max, int val ) { int mid = ( min + max ) / 2 ; if( min > max ) /* NOT FOUND */ return -1 ; if( A[mid] == val ) /* FOUND */ return mid ; if( A[mid] < val ) search( A, ( mid + 1 ), max, val ) ; if( A[mid] > val ) search( A, min, ( mid - 1 ), val ) ; } int main( void ) { int z, max, min, val, index ; int i = 0 ; char A[100] ; char *B ; printf( "BINARY SEARCH ALGORITHM\n" ) ; printf( "Worst Case: O(logn) Best Case: O(1)\n" ) ; printf( "Enter numbers to be searched\n" ) ; while( ( z = getchar() ) != '\n' ) { A[i] = z ; i++ ; } min = 0 ; max = i - 1 ; i = 0 ; B= calloc( max, sizeof( int ) ) ; while( max >= i ) { B[i] = A[i] ; i++ ; } printf( "What number would you like to find?\n" ) ; scanf( "%d", &val ) ; /* changes input value to ascii , to work with the values in the array in the search function */ val = val + 48 ; index = search( B, min, max , val ) ; printf( "Number is at index number %d\n", index ) ; }