binary search program

This is a discussion on binary search program within the C Programming forums, part of the General Programming Boards category; Here are the errors: test3.c: In function ‘main’: test3.c:40: warning: passing argument 1 of ‘binary_srch’ from incompatible pointer type test3.c:42: ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    15

    binary search program

    Here are the errors:
    test3.c: In function ‘main’:
    test3.c:40: warning: passing argument 1 of ‘binary_srch’ from incompatible pointer type
    test3.c:42: warning: suggest parentheses around assignment used as truth value
    test3.c:29: warning: unused variable ‘top’
    test3.c:28: warning: unused variable ‘bottom’
    test3.c:27: warning: unused variable ‘middle’
    test3.c: In function ‘binary_srch’:
    test3.c:57: error: expected declaration specifiers before ‘top’
    test3.c:58: error: expected declaration specifiers before ‘bottom’
    test3.c:60: error: expected declaration specifiers before ‘while’
    test3.c:72: error: expected ‘{’ at end of input


    Here's the code.
    Code:
    
    //prototypes
    int binary_srch ( char arr [], int length, int *found );
    
    
    
    int main( void )
    
    {
    
    
    int arr[] = { 0, 2, 4, 5, 6, 7, 8, 9, 12, 14, 15};
    int value;
    int index;
    int middle;
    int bottom;
    int top;
    int found;
    
    
    found = FALSE;
    
    
    	printf("Enter number to find..\n");
    	scanf("%d",&value);
    
    
    	index =binary_srch (arr,10,&found);
    
    	if(found = TRUE)
    
    		printf("%d found at position %d\n",value,index);
    	else
    		printf("NUMBER NOT FOUND!\n");
    
    
    
    
    return 0;
    }
    
    
    int binary_srch ( char arr [], int length, int *found )
    
    int top;
    int bottom;
    int middle;
    
    
    top = 10;
    bottom = 0;
    
    while ( bottom <= top )
    {  middle = (bottom + top) /2;
    
    	if( arr[middle] > value)	
    		top = middle - 1;
    
    	else if (arr[middle] < value)
    		bottom = middle + 1;
    
    	else  
    		*found = TRUE;
    		return middle;
    }
    Thanks a bunch!

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    15

    Update:

    Alright, I got it to work, but now it is always finding the value at index five....

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by saszew View Post
    Alright, I got it to work, but now it is always finding the value at index five....
    So how many lines of code are you thinking you can stick onto that else section of code, and still think of it as part of the else.

    ???



    You can indent it any crazy (and wrong) way you want. The compiler won't make multiple lines of code work like that without ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Searching a binary search tree - doesn't work
    By Ariod in forum C Programming
    Replies: 1
    Last Post: 08-11-2005, 03:53 PM
  2. Bible program - search functionality
    By ChadJohnson in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 04-15-2005, 11:33 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21