binary search

This is a discussion on binary search within the C Programming forums, part of the General Programming Boards category; hey guys. to the guys who helped with my earlier prob about a bubble sort, this is kind of a ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    28

    binary search

    hey guys. to the guys who helped with my earlier prob about a bubble sort, this is kind of a continuation (although i couldn't solve my probs, i'll seek help at school tomorrow).

    heres the prob. got a 200 customer record structure consisting of first name, last name, address, start and end.

    thats fine, and the early problems i was able to handle by myself. now i need to do a binary search which will read a last name and search for a matching last name within my customer records. this is the code which i came up with. it doesnt work but i am hopeful that i am not far off.

    Code:
    char searchCust(struct customers cust[])
    {
    	int item, location;
    	char searchCust, nums = cust[NUMEL].recSurName[11];
    	system("cls");
    	printf("Please enter the surname of the customer: ");
    	scanf("%s", searchCust);
    	location = binarySearch(nums, NUMEL, item);
    	if (location >-1)
    	{
    		printf("The item was found at index location %d\n", location);
    	}
    	else
    	{
    		printf("The item was not found in the array\n");
    	}
    	return location;
    }
    
    int binarySearch(int list[], int size, int key)
    {
    	int index, found, left, right, midpoint;
    
    	index = -1;
    	found = FALSE;
    	left = 0;
    	right = size - 1;
    	while (left <= right && !found)
    	{
    		midpoint = (int)((left + right)/2);
    		if (key == list [midpoint])
    		{
    			found = TRUE;
    			index = midpoint;
    		}
    		else if (key > list[midpoint])
    		{
    			left = midpoint + 1;
    		}
    		else
    		{
    			right = midpoint - 1;
    		}
    	}
    	return(index);
    }
    i got an error in this line

    Code:
    	location = binarySearch(nums, NUMEL, item);
    it says : error C2664: 'binarySearch' : cannot convert parameter 1 from 'char' to 'int []'

    i tried changing alot of things but i don't actually know what i need to change (i've seen similar errors and eliminated them quickly in the past).

    thanks alot for your time, and your help.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Stop passing it a single character:
    Code:
    char searchCust, nums = cust[NUMEL].recSurName[11];
    Pass it an array like it wants.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    28
    thanks for your help, but i don't fully understand.
    is the passing it as a single character the NUMEL part as NUMEL is defined as 200 which means it only sends the value stored at 200.

    or is it the [11] which i suppose would be sending the 11th letter in the surName field..

    or a combination which sends the 11th letter in the 200th record..
    i've got a feeling its that.

    how can i send it as the whole array, i checked through my book but can't really see how.

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,156
    cust[NUMEL].recSurName[11]

    This is the 11th character in the array. The question is "what array".

    Answer that and you have the answer to

    how can i send it as the whole array...
    And yes, your feeling is correct.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >NUMEL is defined as 200
    >cust[NUMEL].recSurName[11]

    So if the array cust has 200 elements (0-199), you are attempting to access to 201st, which is undefined behavior. Not the problem, but definitely one to consider.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. searching and insertion in a binary search tree
    By galmca in forum C Programming
    Replies: 1
    Last Post: 03-26-2005, 04:15 PM
  2. deleting all nodes of a binary search tree...
    By sachitha in forum C++ Programming
    Replies: 3
    Last Post: 09-29-2004, 06:19 AM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 08:40 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

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